【发布时间】:2021-10-16 16:21:21
【问题描述】:
我想并行运行两个线程(在 python3.6 上),适用于以下代码示例:
import threading
from time import sleep
# use Thread to run def in background
# Example:
def func1():
while True:
sleep(1)
print("Working")
def func2():
while True:
sleep(2)
print("Working2")
Thread(target = func1).start()
Thread(target = func2).start()
但它不适用于线程。线程:
import threading
from time import sleep
# use Thread to run def in background
# Example:
def func1():
while True:
sleep(1)
print("Working")
def func2():
while True:
sleep(2)
print("Working2")
x = threading.Thread(target=func1())
y = threading.Thread(target=func2())
x.start()
y.start()
我想使用后一个选项来检查 x 或 y 是否还活着。
【问题讨论】:
标签: python python-3.x multiprocessing python-multithreading