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?
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?
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.
Win 10, 64 bit using Python 3.6
I'd suggest using task scheduler to set a time to run a batch file that contains a command line to run your script.
Here a very naive approach:
Another tutorial (not much different from first one):

Instead of using python.exe or a .bat file, you should use py.exe -3.6
(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()
showinfo("Hello from Python", sys.version)
My code examples are always for Python >=3.6.0
Almost dead, but too lazy to die:
All humans together. We don't need politicians!
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)

schedule.every(10), job)
schedule.every(10), job)
schedule.every(10), job)

while 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).

Possibly Related Threads…
Thread Author Replies Views Last Post
  Can you end the Time.sleep function boier96 9 192 Jan-16-2021, 10:09 PM
Last Post: Serafim
  Difference between os.system("clear") and os.system("cls") chmsrohit 7 5,448 Jan-11-2021, 06:30 PM
Last Post: ykumar34
  In this function y initially has no value, but a call to foo() gives no error. Why? Pedroski55 8 336 Dec-19-2020, 07:30 AM
Last Post: ndc85430
Sad Function defined by branches antoniogalante 1 135 Dec-16-2020, 11:35 PM
Last Post: deanhystad
  Struggling for the past hour to define function and call it back godlyredwall 2 266 Oct-29-2020, 02:45 PM
Last Post: deanhystad
  list call problem in generator function using iteration and recursive calls postta 1 201 Oct-24-2020, 09:33 PM
Last Post: bowlofred
  Function will not return variable that I think is defined Oldman45 6 557 Aug-18-2020, 08:50 PM
Last Post: deanhystad
  Having hard time understanding the function self-returning itself twice jagasrik 2 364 Aug-15-2020, 08:50 PM
Last Post: deanhystad
  How to call/read function for all elements in my list in python johnny_sav1992 1 380 Jul-27-2020, 04:19 PM
Last Post: buran
  How do I find if a function has been defined? AndyHolyer 3 421 Jul-24-2020, 01:39 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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