python如何同时运行两个函数

lewis 2019-03-14 23次阅读

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

def fun1():

while True:

time.sleep(2)

print("fun1")

def fun2():

while True:

time.sleep(6)

print("fun2")

threads = []

threads.append(threading.Thread(fun1))

threads.append(threading.Thread(fun2))

print(threads)

if __name__ == '__main__':

for t in threads:

print(t)

t.start()



发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。