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
  time function does not work tester_V 4 265 Oct-17-2021, 05:48 PM
Last Post: tester_V
  Why recursive function consumes more of processing time than loops? M83Linux 9 1,382 May-20-2021, 01:52 PM
Last Post: DeaD_EyE
  how to call an object in another function in Maya bstout 0 508 Apr-05-2021, 07:12 PM
Last Post: bstout
  Can you end the Time.sleep function boier96 9 1,245 Jan-16-2021, 10:09 PM
Last Post: Serafim
  Difference between os.system("clear") and os.system("cls") chmsrohit 7 8,716 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 1,042 Dec-19-2020, 07:30 AM
Last Post: ndc85430
Sad Function defined by branches antoniogalante 1 547 Dec-16-2020, 11:35 PM
Last Post: deanhystad
  Struggling for the past hour to define function and call it back godlyredwall 2 798 Oct-29-2020, 02:45 PM
Last Post: deanhystad
  list call problem in generator function using iteration and recursive calls postta 1 650 Oct-24-2020, 09:33 PM
Last Post: bowlofred
  Function will not return variable that I think is defined Oldman45 6 1,388 Aug-18-2020, 08:50 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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