0
点赞
收藏
分享

微信扫一扫

Python简单的多线程例子

我阿霆哥 2022-03-11 阅读 64
python

示例代码

import threading
import time
from datetime import datetime
from concurrent.futures.thread import ThreadPoolExecutor


def tell(i):
    a = "end " + str(i)
    print(a)
    print(datetime.now())
    print(threading.currentThread())
    time.sleep(10)


def go():
    excutors = ThreadPoolExecutor(max_workers=3)

    for i in range(0, 999):
        excutors.submit(tell, i)
        print(i)

    excutors.shutdown()


if __name__ == '__main__':
    go()

输出如下

end 16
2022-03-01 21:45:12.995054
<Thread(ThreadPoolExecutor-0_0, started daemon 140558245992192)>
end 17
2022-03-01 21:45:12.995218
<Thread(ThreadPoolExecutor-0_2, started daemon 140558229206784)>
end 18
2022-03-01 21:45:23.003328
<Thread(ThreadPoolExecutor-0_0, started daemon 140558245992192)>
end 19
2022-03-01 21:45:23.004750
<Thread(ThreadPoolExecutor-0_2, started daemon 140558229206784)>
end 20
2022-03-01 21:45:23.005117
<Thread(ThreadPoolExecutor-0_1, started daemon 140558237599488)>
end 21
2022-03-01 21:45:33.008747
<Thread(ThreadPoolExecutor-0_0, started daemon 140558245992192)>
end 22
end 23
2022-03-01 21:45:33.009060
2022-03-01 21:45:33.009099
<Thread(ThreadPoolExecutor-0_2, started daemon 140558229206784)>
<Thread(ThreadPoolExecutor-0_1, started daemon 140558237599488)>
end 24
举报

相关推荐

0 条评论