Forums

Question with long-running processes

Hi -- I am finding the explanation for long-running processes confusing: https://help.pythonanywhere.com/pages/LongRunningTasks

Am I supposed to put that code snippet at the top of my python script (the one that operates my whole program, which I want to run continuously?)

When I do so, and run a scheduled task, the task log shows the following error:

ImportError: No module named my_module

(...or is this supposed to be running in a different script from the main one and "my_module" is supposed to be replaced with a path to my main script? Or something like that?...)

Any clarification would be appreciated. Thank you!

The code snippet is meant to be something in a file of its own, and my_module.my_long_running_process is the code that you want to run. Then you run the file with the code snippet in it, and it will do the thing with the lock to make sure that no other instances are running on the same machine, then if all is OK, it will call your code.

So for example I have a scrpt called ping.py

So the long running code should be from my_module import ping.py ?

No, you'll want to import whichever function/class from ping.py that does the work that you want to do and then call that in the long-running task code. my_module in the example is a placeholder.

There is a nice description of the theory of Python imports here. You'll want to understand that so that you can work out what you need to import and how to import it.