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()
浙公网安备 33010602011771号