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()
# 传递一个参数可以使用元组或数组
threading.Thread(target=reste, args=('parm1',)).start()  #注意 逗号要有 元组中只包含一个元素时,需要在元素后面添加逗号。
threading.Thread(target=reste, args=['parm1','parm2']).start() # 同理,多个参数时,也可以使用数组

python 线程池之submit

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)
            # 传入多个方法参数时使用
            # threadPool.submit(lambda p: data_check2(*p), [param1, param2])

python 线程池之map,一个参数

from concurrent.futures import ThreadPoolExecutor
from loguru import logger

def main1(i):
    logger.info('我是第' + str(i) + '页')
    time.sleep(3)

def thred_map():
    iss = []
    for i in range(1, 50):
        iss.append(i)
    pool = ThreadPoolExecutor(10)
    pool.map(main1, iss)

if __name__ == '__main__':
    thred_map()

python 线程池之map,多个参数

from concurrent.futures import ThreadPoolExecutor
from loguru import logger

def main2(i, m):
    logger.info('我是第{}页,{}', i, m)
    time.sleep(2)


def thred_map():
    data_list = [(1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6), (7, 7), (8, 8), (9, 9), (10, 10)]
    pool = ThreadPoolExecutor(3)
    pool.map(job1, data_list)


def job1(lists):
    main2(lists[0], lists[1])


if __name__ == '__main__':
    thred_map()

posted @ 2021-12-09 22:46  darling331  阅读(237)  评论(0编辑  收藏  举报