Home > Net >  Python Threading Timer - activate function every X seconds
Python Threading Timer - activate function every X seconds

Time:04-21

Is there any simple way to activate the thread to fire up the function every X sec, to display some data?

def send_data():
    data = "Message from client"
    socket.sendall(data.encode())


write_thread = threading.Thread(target=send_data())
write_thread.start()

CodePudding user response:

You could try the ischedule module - it provides very straightforward syntax for scheduling any given function.

Here's an example straight from the GitHub page:

from ischedule import run_loop, schedule


@schedule(interval=0.1)
def task():
    print("Performing a task")


run_loop(return_after=1)

The return_after param in run_loop() is an optional timeout. Also, in case you're unfamiliar, the @ syntax is a Python decorator.

CodePudding user response:

A simple way would be this: `import time

while True: task() time.sleep(1)`

  • Related