Forums

threads

I need to run a check function 5 minutes after a payment request was made. I've done some research. I believe I could use celery, tho it's a heavy module.

I remembered I could use a python thread with a 5 minutes timer.

Since I have never done this in this context, I wonder if this is reasonable to do on web server?

or if there are better alternatives?

Thank you, Jaime

Threads won't work in a web app, but we have a help page with some ideas:

https://help.pythonanywhere.com/pages/AsyncInWebApps/

thank you for the link!

I tried django background tasks, however I could not get to where I want. In order to run the tasks I need to run a command line:

python manage.py process_tasks

otherwise tasks don't seem to run,

this is strange,

it should run in real time or no?

Thanks for posting that! We hadn't heard of Django Background Tasks, and it looks like an awesome solution for this kind of thing. We'll take a closer look and perhaps add it to the help page, with some detailed instructions.

Anyway, yes, you do need to have something running the process_tasks management command in order for it to work. I'd suggest that you set it up as an always-on task (on the "Tasks" page) instead of running it in a console, though. This is the PythonAnywhere equivalent of the suggestion they make in the docs saying "use a grown-up program like supervisord to handle this for you" -- always-on tasks are very similar to supervisord in the way they work.

The command to use in the always-on task setup will need to do all of the stuff that you do in a console to run it, separated by &&. So, for example:

workon myvirtualenv && cd ~/mysite/ && python manage.py process_tasks

...where you'll need to replace myvirtualenv and mysite with the actual names of the virtualenv and the directory where your manage.py lives.

well Django Background Tasks are mentioned in this link:

https://help.pythonanywhere.com/pages/AsyncInWebApps/

thank you a lot for your answer, it seems to be working now just the way I want! Awsome! I'm happy.

Gosh, I'd not realised it was in our help page -- one of my colleagues must have found out about it and added the link. Very glad to hear it's all working now :-)

Hello Giles, I probably miss something about your solution. If I want to check every minute that a task need to be done, how can I do it with always-on task that can be setup only hourly or daily? Thanks

There are two kinds of tasks -- scheduled tasks, which run either hourly or daily, and always-on tasks, which are scripts that you want to have running forever.

An always-on task that checked if something needs to be done every minute might have code like this (assuming that the thing that you are doing every minute rarely takes more than a minute to run):

while True:
    start = time.time()
    if thing_needs_to_be_done():
        do_thing()
    time_to_taken = time.time() - start
    time_to_wait = 60 - time_taken
    if time_to_wait > 0:
        time.sleep(time_to_wait)

My bad, I haven't seen "the always on task". Thanks for the quick answer

No problem, glad to help!