网络编程
位置:首页>> 网络编程>> Python编程>> python创建线程示例

python创建线程示例

  发布时间:2021-07-21 00:20:37 

标签: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个线程是交替的执行的

 

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com