How to run 2 functions at the same moment Python3.10

Viewed 24

Im trying to make an input with a function that detects when the current minute is 1, by writting the following code :

import datetime

time = datetime.datetime.now()

def update_time():
    while True:
        time = datetime.datetime.now()
        if time.minute == 1:
            quit()

def script():
    while True:
        input("Something ... ")

The exemple is very weird and useless but is easier to explain than the thing that I really want.

Can someone tell me how to run the update_time() and the script() at the same time?

1 Answers

Use Thread:

import datetime
import threading

time = datetime.datetime.now()

def update_time():
    while True:
        time = datetime.datetime.now()
        if time.minute == 1:
            quit()

def script():
    while True:
        input("Something ... ")

t1 = threading.Thread(target=update_time, args=())
t2 = threading.Thread(target=script, args=())
t1.start()
t2.start()
Related