例子,python创建线程
复制代码 代码示例:
import threading
from time import sleep
def test_func(id):
for i in range(0,5):
sleep(1)
print('thread %d is running %d' % (id,i))
threads = []
for i in range(0,3):
t = threading.Thread(target=test_func, args=(i,))
threads.append(t)
for t in threads:
t.start()
for t in threads:
t.join()
大家可以测试下,从输出结果可以看出,3个线程是交替的执行的,也就是应用了python线程操作。