本文主要是介绍Python 多线程和线程池,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
python 多线程
import threading
from time import sleep
def reste(chapter, chapter_herf, sleeptime):
sleep(sleeptime)
print('===================')
# doSomeThing
# content = get_contents(chapter_herf)
# write_txt(chapter, content, 'utf8')
t1 = threading.Thread(target=reste, args=('parm1', 'parm2', 1))
t1.start()
t2 = threading.Thread(target=reste, args=('parm1', 'parm2', 1))
t2.start()
threading.Thread(target=reste, args=('parm1', 'parm2', 1)).start()
python 线程池
from concurrent.futures import ThreadPoolExecutor
from loguru import logger
def main(i):
logger.info('我是第' + str(i) + '页')
time.sleep(3)
if __name__ == '__main__':
# 10 表示开启10个线程池
with ThreadPoolExecutor(10) as threadPool:
for i in range(1, 683):
threadPool.submit(main, i)
# threading.Thread(target=main, args=[i]).start()
这篇关于Python 多线程和线程池的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!