扫描二维码下载沐宇APP

沐宇

微信扫码使用沐宇小程序

沐宇

python如何同时运行两个函数

扬州沐宇科技
2021-03-01 18:55:50
python

python如何同时运行两个函数

在python中利用线程同时运行两个函数,具体方法如下:

def fun1():

while True:

time.sleep(2)

print("fun1")

def fun2():

while True:

time.sleep(6)

print("fun2")

threads = []

threads.append(threading.Thread(target=fun1))

threads.append(threading.Thread(target=fun2))

print(threads)

if __name__ == '__main__':

for t in threads:

print(t)

t.start()

扫码添加客服微信