python-多任务编程-线程池

示例代码如下:

import time
from concurrent.futures import ThreadPoolExecutor


def make_food(food):
    print(food, "开始制作")
    time.sleep(2)
    print(food, "制作完成")
    return food


def main():

    food_list = ['番茄炒鸡蛋', '青椒炒牛肉']

    # 创建2个线程
    
    # pool = ThreadPoolExecutor(max_workers=2)
    #
    # res = pool.map(make_food, food_list)
    #
    # for item in res:
    #     print(item)
    #
    # pool.shutdown()

    with ThreadPoolExecutor(max_workers=2) as pool:

        res = pool.map(make_food, food_list)

        for item in res:
            print(item)
        

if __name__ == '__main__':
    main()
posted @ 2021-03-26 17:00  程序员陈师兄cxycsx  阅读(99)  评论(0)    收藏  举报