栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python 多线程并行化

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python 多线程并行化

import time
import threading
from concurrent.futures import ThreadPoolExecutor
#method对应于:自行编写的方法,可传参(可传多个参数,形如pool.submit(self.method, a,b))
def method1():
	print("我是方法1")
def method2():
	print("我是方法2")
def method3():
	print("我是方法3")
def method4():
	print("我是方法4")
def method5():
	print("我是方法5")
def fun():
    start=time.time()
    # 创建包含5个线程的线程池
    pool = ThreadPoolExecutor(max_workers=5)
    future1 = pool.submit(method1)
    future2 = pool.submit(method2)
    future3 = pool.submit(method3)
    future4 = pool.submit(method4)
    future5 = pool.submit(method5)

    print("方法1结果:",future1.result())
    print("方法2结果:",future2.result())
    print("方法3结果:",future3.result())
    print("方法4结果:",future4.result())
    print("方法5结果:",future5.result())
    end=time.time()
    print("总耗时:",end-start)
    # 关闭线程池
    pool.shutdown()
fun()
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/822491.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号