Forums

Always-on task

How long does an always-on task take to start? In order to start a simple code with literally print('JAJAJA') in it takes 3 minutes to get prepared and started for the first time.... I am really confused. I could not understand why my code takes so much time in "Starting" without any single action (about an hour). P.s. I do have time that I do not fulfill - about 1.5K seconds all the time.

Does your script contain anything apart from just the print statement? If so, it will start up, print something, and then exit. The system will then restart it, and it will print something and exit again, and so on. The net effect that you will see is that it will always be in a "starting" state because the only time it is "running" is while it's printing -- that is, for a couple of milliseconds between restarts. Most of the time it will be waiting to be restarted. And because it keeps apparently crashing, the always-on system will eventually assume that it's just broken and will slow down the speed at which it restarts it.

Always-on tasks are designed for programs that run for a long time, not for scripts that do something then immediately exit.

ok good point. No and yes. This particular example I made to see if it works whatsoever. I launched the process, it printed that is was starting and in 3 minutes it printed out the output (afterwards it took about 30-50 seconds to relaunch). The other program I have has timers and an infinite while loop. I will double check, but it looks like the first preparation for python code takes quite a long time. Also I tried to estimate the time it takes to "import time" library - on average it takes 5 seconds out of CPU time, which is kind of confusing to be honest.... It is a quite small and simple lib.

I think that anything <1min to startup should be about par for the first startup.

How are you estimating the cpu time? Keep in mind that that is very different from wall time.

Thought I might add some information. I have been running an always-on task for a few weeks whose sole purpose is to use selenium to scrape the PythonAnywhere dashboard page and dump the CPU used/remaining etc info to a json file so I can access it from my python code.

Over that time for various reasons there have been exceptions and the task has had to end.

In general it is up again in less than five minutes. Sometimes a bit longer. Once it was a lot longer but I haven't troubleshooted it yet so is probably my bad.

In any case I thought I'd provide some real-world usage info. I use sentry to alert me if the task stays down for any length of time and that all seems to work ok.

I am really liking these always-on-tasks.

:)

Excellent, glad it's working out well! It should generally take much less than five minutes to restart a task when it crashes -- I have a test task that deliberately "crashes" from time to time that I use for monitoring, and it looks like it's taking about 30 seconds most of the time. It's the initial task startup that is still rather slow, up to five minutes.

Hi, I'm trying to make a task for my script to work in Always-In-Tasks, but this function does not work correctly, since all files are created not in the script folder, but in the home folder /justluckylocky where all my script folders are located. If you use venv you get the same result

You need to control where you write your files by using absolute paths.

I’m using absolute paths with os.getcwd() + mypath

Could we look into your code? We can, but we ask first.

No, thanks. I'll try to fix it myself

if i run my code as usual,is my code always running?

Do you mean code that you run in a console? It won't run forever, as we sometimes have to reboot servers for system maintenance. When that happens, the code will stop running and you would have to log in again to restart it.

Hello, I am using the always on task...its state is running, but still when i call the endpoint (I have a flask app) it gives the 504-load balancer error after nearly 5 mins. Why is it happening even though the always on task function is running?

If I understand correctly, that's an issue with your app, not the always-on task. Check how the app behaves (have a look at error / server logs). Also, we have a 5 min timout set on processing requests, so if your web app performs some heavy processing that takes more than 5 mins it would be killed.