如何在每5秒的时间间隔内执行两个函数,直到计时器停止在Python中使用线程。
e.g:
def first_func():
print("First function")
def second_func():
print("Second function")
Result should be like this:
-------------------------------
First Function At starts
Second Function 5 Seconds
First Function 10 Seconds
Second Function 15 Seconds
First Function 20 Seconds
Second Function 25 Seconds
First Function 30 Seconds
And so on.
答案 0 :(得分:0)
尝试使用多处理
def you_function1(*args, **kwargs):
pass
def you_function2(*args, **kwargs):
pass
from multiprocessing import Process
first_process = Process(target=you_function1)
second_process = Process(target=you_function2)
first_process.start()
second_process.start()
first_process.join()
second_process.join()
答案 1 :(得分:0)
线程非常简单:
import threading
import time
def first_func():
while True:
print("First function",time.time()-start)
time.sleep(10)
def second_func():
time.sleep(5)
while True:
print("Second function",time.time()-start)
time.sleep(10)
start = time.time()
t1 = threading.Thread(target=first_func)
t1.start()
t2 = threading.Thread(target=second_func)
t2.start()
输出:
First function 0.0
Second function 5.008664846420288
First function 10.002728939056396
Second function 15.010392904281616
First function 20.018056869506836
Second function 25.02572202682495