Making a program wait until asked to do something

Discussion in 'Programming & Software Development' started by schnappy, Sep 12, 2020.

  1. schnappy

    schnappy Member

    Joined:
    Apr 23, 2008
    Messages:
    843
    Location:
    3124
    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
     
  2. neRok

    neRok Member

    Joined:
    Aug 19, 2006
    Messages:
    3,938
    Location:
    Perth NOR
    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?
     
  3. OP
    OP
    schnappy

    schnappy Member

    Joined:
    Apr 23, 2008
    Messages:
    843
    Location:
    3124
    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.
     
  4. waltermitty

    waltermitty Member

    Joined:
    Feb 19, 2016
    Messages:
    1,440
    Location:
    BRISBANE
  5. Murmandamus

    Murmandamus Member

    Joined:
    Jun 27, 2001
    Messages:
    1,094
    Location:
    Sydney
  6. Renza

    Renza Member

    Joined:
    Dec 1, 2004
    Messages:
    4,809
    Location:
    Melbourne
    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.
     
  7. OP
    OP
    schnappy

    schnappy Member

    Joined:
    Apr 23, 2008
    Messages:
    843
    Location:
    3124
    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
     

Share This Page

Advertisement: