VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 编程开发 > python爬虫 >
  • Threading线程实例4

import threading
from time import sleep, ctime
loops = [1, 2, 3, 4, 5]
def loop(i, delay):
    print(f'线程第 {i+1} 次开始运行: {ctime()}\n')
    sleep(delay)
    print(f'线程第 {i+1}次运行成功: {ctime()}\n')
def main():
    print('开始测试', ctime())
    threads = []
    nloops = range(5)
    for i in nloops:
        t = threading.Thread(target=loop, args=(i,loops[i]))
        threads.append(t)
    for i in nloops: # 开始线等
        threads[i].start()
    for i in nloops: # 线程等待
        threads[i].join()
    print(f'主线程关闭: {ctime()}')
if __name__ == '__main__':
    main()



相关教程