python如何同时运行两个函数

1188
2021/3/1 18:55:50
栏目: 编程语言
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

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()

辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读: python怎么查看内存占用情况