Hi, I have a small collection of python scripts I have prepared that I want each to run periodically. Usually I have just done an infinite while loop with a wait(5 seconds) at the end which has been fall proof. I am wanting to daemonise these on Windows and it seems the most maintainable way to do it is to use Task Scheduler to handle the periodic behaviour and the need to daemonise. Some of these scripts however work better if they are running all the time. E.g. one is a web scraper and it is more reliable if it holds a session all the time like leaving a browser open rather than creating a new session every 30mins. How can I have my python script running all the time, but only do my thing on a Task Scheduler set interval? I'm a shitty programmer I only do simple things and it gets me by, my best guess is to have each script where I need this functionality bind a socket and then the Task Scheduler command will send a heartbeat to the socket. I don't know how to program this so would rather not spend all day on doing this. Are there any other simple ways? I feel like it's a simple problem with a simpler solution but can't find the right googleable words. Thanks
I don't know the right answers, but I think you're on the right track with using a socket or something like that. I guess you would need a second small script to run on demand and ping the daemon. With regards to the loop, you should look at using more suited to the task, like the async features in py3. They would probably be more efficient, presuming they use OS features for the waiting, rather than just waiting. With regards to the session, maybe you can save the "context" to a file and load it on demand?
Thanks for the reply, Saving the context to a file is the solution I've been aiming for if I can be bothered, otherwise the lazy 'do nothing' solution will probably be ok for the next few weeks. Having sockets just feels complex and one big goal is best I can make the program as easy to maintain as possible so I can walk away from it, which is inherent to simplicity.
I'd use some sort of notification/queuing system. I'm used to SNS/SQS in AWS, but you could just as easily use something like RabbitMQ or MSMQ since you're on windows. So your main application will listen to events coming from the queue. You then have your task scheduler run a task to put things onto the queue whenever you wish to do something.
Thanks for all the interesting ideas everyone. I thought it'd be messy and awkward, but it turns out saving the context to disk is trivial using pickle - I got that running in about a minute: https://github.com/psf/requests/issues/2522
Easiest thing I can think of is either hard code in an interval with the pause, or read a reg key for the schedule eg if regkey shedule time =now +/- 5mins, do thing else sleep until next shedule Oops. sorry for necro
1. write a proper daemon https://python-service.readthedocs.io/en/stable/ 2. write it as a listener https://www.geeksforgeeks.org/python-binding-and-listening-with-sockets/ if periodical (run every X), still write as a daemon but have it execute off the scheduler, if it is long run (runs longer than interval) the daemon will stop multiple instances of the same code.
Thanks for all the suggestions. I am constrained to a Windows server which I had lots of issues doing what I initially intended - daemon running all the time, periodic interval. What is working great is using Task Scheduler for the scheduling and pickle to save and load the context which removes the requirement to have it running all the time.