pikastech 6d4b7a3eb7 enhance event ant thread
enhance _thread start
2023-02-26 17:04:58 +08:00

33 lines
636 B
Python

import _thread
import time
task1_finished = False
task2_finished = False
def task1():
global task1_finished
print("task1")
for i in range(10):
time.sleep(0.05)
print("task1")
task1_finished = True
def task2(sleep_time, loop_count):
global task2_finished
print("task2:", sleep_time, loop_count)
for i in range(loop_count):
time.sleep(sleep_time)
print("task2")
task2_finished = True
_thread.start_new_thread(task1, ())
_thread.start_new_thread(task2, (0.05, 10))
while not task1_finished or not task2_finished:
pass
time.sleep(0.5) # wait for threads to exit