Python Forum
function call at defined system time?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
function call at defined system time?
#1
Hello everyone,

I want to create a background service like script, that will execute tasks at given times and/or dates. I would like to use asyncio for that, since I will have a loop going anyway. But I have hard times to find a way to monitor system time and trigger events at a given time.

Any idea how to do that?
Reply
#2
It would help to know which operating system you're running, as they tend to have their own ways of doing these sorts of things.
Reply
#3
Win 10, 64 bit using Python 3.6
Reply
#4
I'd suggest using task scheduler to set a time to run a batch file that contains a command line to run your script.
Reply
#5
Here a very naive approach: https://datatofish.com/python-script-windows-scheduler/
Another tutorial (not much different from first one): https://www.jcchouinard.com/python-autom...scheduler/

Instead of using python.exe or a .bat file, you should use py.exe -3.6 your_program.py.
(Python 3.9 was released yesterday)

If your application is not a console application or if you want to hide the console, use instead of py.exe pyw.exe.

For testing it, I've used this code:
import sys
from tkinter import Tk
from tkinter.messagebox import showinfo


root = Tk()
root.withdraw()
showinfo("Hello from Python", sys.version)
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#6
There are also libraries that work fine for like schedule, APScheduler, pycron
The two first have i used serval times before,Python job scheduling for humans is maybe the easiest to use.

Holon Wrote:I would like to use asyncio for that
That's maybe overkill for this task,as usually don't need 1000's task to load at same time for this.
Schedule use simpler Threading if that's needed.
import threading
import time
import schedule


def job():
    print(f"I'm running on thread {threading.current_thread()}")

def run_threaded(job_func):
    job_thread = threading.Thread(target=job_func)
    job_thread.start()

schedule.every(10).seconds.do(run_threaded, job)
schedule.every(10).seconds.do(run_threaded, job)
schedule.every(10).seconds.do(run_threaded, job)

while 1:
    schedule.run_pending()
    time.sleep(1)
APScheduler start separate Thread automatic with schedulers.background
apscheduler Wrote:A scheduler that runs in the background using a separate thread (start() will return immediately).
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Variable is not defined error when trying to use my custom function code fnafgamer239 4 515 Nov-23-2023, 02:53 PM
Last Post: rob101
  Printing the variable from defined function jws 7 1,165 Sep-03-2023, 03:22 PM
Last Post: deanhystad
  return next item each time a function is executed User3000 19 2,174 Aug-06-2023, 02:29 PM
Last Post: deanhystad
  How do I call sys.argv list inside a function, from the CLI? billykid999 3 753 May-02-2023, 08:40 AM
Last Post: Gribouillis
  Getting NameError for a function that is defined JonWayn 2 1,056 Dec-11-2022, 01:53 PM
Last Post: JonWayn
Question Help with function - encryption - messages - NameError: name 'message' is not defined MrKnd94 4 2,776 Nov-11-2022, 09:03 PM
Last Post: deanhystad
  How to print the output of a defined function bshoushtarian 4 1,237 Sep-08-2022, 01:44 PM
Last Post: deanhystad
  User-defined function to reset variables? Mark17 3 1,590 May-25-2022, 07:22 PM
Last Post: Gribouillis
  Mock obj - How to call the side_effect every time during the run? pythonisbae 3 2,556 Mar-06-2022, 09:37 AM
Last Post: pythonisbae
  time function does not work tester_V 4 2,946 Oct-17-2021, 05:48 PM
Last Post: tester_V

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020