PythonAnywhere Forums

Always crashing when On profit..

Hey Python anywhere, are you somehow related to Poloniex? I just notice my program is always crashing when I am "on Profit" but console is not crashing or working properly when I am loosing. I am using console to start with here mkvirtualenv my-virtualenv --python=python3.6. Then run my program there.

You cannot tell me it is my program has problem because my console is not crashing when I am loosing or when the price is going down and sometimes it is working properly when the price is up but usually console is crashing.

Sometimes console servers have to be restarted for maintenance, or crash due to load -- for that reason we recommend that you don't use them for programs that you want to keep running constantly. This help page explains how to set up a scheduled task so that your program keeps running forever.

I am new to python and I dont know how to put my code there. My codes directory is here /home/myusername/myfolder/my_func.py

How can I set lock_id? Is the task name equivalent to my_func? Something like this?

lock_id = "myusername.my_func"?

How can I insert it? Something like this?

import logging

import socket

import sys

lock_socket = None   
def is_lock_free():

    global lock_socket
    lock_socket = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
    try:
        lock_id = "myusername.my_func" 
        lock_socket.bind('\0' + lock_id)
        logging.debug("Acquired lock %r" % (lock_id,))
        return True
    except socket.error:
        # socket already locked, task must already be running
        logging.info("Failed to acquire lock %r" % (lock_id,))
        return False

def my_func():

    x = 10
    print("Value inside function:",x)

if not is_lock_free():

    sys.exit()

my_func()

Kindly walk me through on this

[edited by admin: formatting]

yup- something like that sounds correct. lock_id is just a unique identifier, that we suggest to be username.function_name

It could be anything.

And after that I can save that like anything.py in my folder and run in bash console in mkvirtualenv my-virtualenv --python=python3.6? Is that correct?

if you want to run a scheduled task, you should add that task under the "schedule" tab on the PythonAnywhere dashboard.

To make a scheduled task long running, you should schedule it to run say every 15min, and to use that locking code to make sure that there are no duplicate runs (ie. if the code is already running, exit early, otherwise run the code).

It says "Task has not been run yet" even after an hour.

hi there, are you still seeing problems? i'm currently seeing some content in your task logs. (including an import error?)

I already figure out the solution thats why you see logs. Anyway, my code is not running coz I cant install wrapper of poloniex. Unlike in consoles it is installing. I try to pip the wrapper and it says permission denied in the tasks. Is there any solution?

If you can install it in a console, then it will be availabe in a scheduled task as long as you use the same version of Python.
try specifying the python version explicitly in your scheduled task command, so eg

python3.6 /home/myusername/myscript.py

instead of just myscript.py...

Here is what it says when i run pip3 install https://github.com/s4w3d0ff/python-poloniex/archive/v0.4.6.zip

Collecting https://github.com/s4w3d0ff/python-poloniex/archive/v0.4.6.zip Downloading https://github.com/s4w3d0ff/python-poloniex/archive/v0.4.6.zip Requirement already satisfied: requests in /usr/local/lib/python3.6/dist-packages (from poloniex==0.4.6) Installing collected packages: poloniex Running setup.py install for poloniex: started Running setup.py install for poloniex: finished with status 'error' Complete output from command /usr/local/bin/python3.6 -u -c "import setuptools, tokenize;file='/tmp/pip-vvmpn89x-build/setup.py';f=getattr(tokenize, 'open', open)(file);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, file, 'exec'))" install --record /tmp/pip-zp6s88od-record/install-record.txt --single-version-externally-managed --compile: running install running build running build_py creating build creating build/lib creating build/lib/poloniex copying poloniex/init.py -> build/lib/poloniex copying poloniex/coach.py -> build/lib/poloniex running install_lib creating /usr/local/lib/python3.6/dist-packages/poloniex error: could not create '/usr/local/lib/python3.6/dist-packages/poloniex': Permission denied

----------------------------------------

Command "/usr/local/bin/python3.6 -u -c "import setuptools, tokenize;file='/tmp/pip-vvmpn89x-build/setup.py';f=getattr(tokenize, 'open', open)(file);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, file, 'exec'))" install --record /tmp/pip-zp6s88od-record/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /tmp/pip-vvmpn89x-build/

2017-11-11 10:01:56 -- Completed task, took 66.00 seconds, return code was 1.

When i run python3.6 /home/myuser/test4.py...

Traceback (most recent call last): File "/home/myuser/test4.py", line 1, in <module> from poloniex import Poloniex ModuleNotFoundError: No module named 'poloniex'

2017-11-11 09:53:07 -- Completed task, took 3.00 seconds, return code was 1.

Kindly tell me there is solution to this since it is really crashing when i needed it most to run especially when im asleep.

You want pip3.6 install --user poloniex. See this page for more info on installing modules; http://help.pythonanywhere.com/pages/InstallingNewModules

Now it says

TypeError: 'Poloniex' object is not callable

Any other solution?

well that's a different error! are you now getting the same error in the Bash console as in the webapp? if so, it's probably something to do with the way you're using the poloniex module...

I am not getting that error in the console.. Even in the mkvirtualenv.. I am using the same exact code with no errors in console. The only difference is just I pasted the code above with is_lock_free(). How come this Task thing is so error prone?

can i have permission to look at your files? we can do it via our admin view but we always ask first...

Yes please and make it work..

When I check in a Bash console, I see the exact same error as is being reported in the scheduled task:

python3.6 /home/rrl/rrl1/test3.py
Traceback (most recent call last):
  File "/home/rrl/rrl1/test3.py", line 279, in <module>
    bal = polo('returnAvailableAccountBalances')
TypeError: 'Poloniex' object is not callable

looks like you have a bug in your code. That's nothing to do with our system, and I can't fix it for you I'm afraid...

Then why the same exact code is running perfectly in bash console without TypeError ?

it's not. I just tried it. I got the exact same TypeError in a Bash console. perhaps we're not running the exact same thing?

I ran:

python3.6 /home/rrl/rrl1/test3.py

check your consoles page and you'll be able to see the console i just used.

Its because you didnt run this

12:21 ~ $ mkvirtualenv my-virtualenv --python=python3.6

After I run that and cd my path, it is working..

Then you need to use your virtualenv in the scheduled task. See http://help.pythonanywhere.com/pages/VirtualEnvInScheduledTasks/

Finally, real solution that works.