Python从线程获取函数返回值
Python中利用强大的threading模块可以很容易的实现多线程开发,提高运行速度。这一般是对某个进行大量计算操作的的函数进行多线程处理,然后合并各线程的结果。获取函数返回值的方法可以如下:
1). 利用multiprocessing.pool类
import time
import random
def test1(): # run without multi-thread
t = time.time()
list = []
for i in range(10000000):
list.append(random.choice([0,1,2,3,4,5])) # operation
print time.time()-t
return list
def test2(): # run with multi-thread
from multiprocessing.pool import Pool
def func(x):
return random.choice([0,1,2,3,4,5])
t = time.time()
pool = Pool(processes=4) #线程数
results = pool.map_async(func, range(10000000)) # 用法同map,与之相似的还有apply和apply_async
print time.time()-t
return results
r1 = test1()
r2 = test2()
运行结果为7.6s和4.2s。可以看到结果并非线性地减少4倍,这可能与运行结果需要同步有关(没深入研究,猜的)。
2). 利用threading类
def func2(m, results, index):
for i in range(m):
result[index].append(random.choice([0,1,2,3,4,5]))
from threading import Thread
def test4():
threads = [None] * 4
results = [[] for i n range(4)]
for i in range(4):
threads[i] = Thread(target=func2, args=(2500000, results, i))
threads[i].start() # 开始线程
for i in range(4):
threads[i].join() # 等待线程结束后退出
return results
不过要注意的是只有在处理需要占用大量内存的数据的时候才考虑多线程,因为线程之间的同步问题很重要,因此设计函数的时候就需要注意,否则处理不好反而会产生很多问题。
参考
PS:threading模块和multiprocessing模块的文档的说明十分详细,准备好好研究下。
- http://stackoverflow.com/questions/17262095/speed-up-a-single-task-using-multi-processing-or-threading
- http://stackoverflow.com/questions/6893968/how-to-get-the-return-value-from-a-thread-in-python
- https://www.tutorialspoint.com/python/python_multithreading.htm
Python从线程获取函数返回值的更多相关文章
- PHP获取函数返回值的引用
通过在函数前添加&可以获取函数返回值的引用,如:function &test(){return 10;}
- Shell $?获取函数返回值或者上一个命令的退出状态
Shell $?获取函数返回值或者上一个命令的退出状态 来自:互联网 时间:2021-01-12 阅读:2 $? 是一个特殊变量,用来获取上一个命令的退出状态,或者上一个函数的返回值. 所谓退出状态, ...
- Python学习教程(learning Python)--2.3.4Python函数返回值
本节讨论Python函数返回值问题. Python和C语言一样,也可以在函数结束时返回一个值.但在定义自己的Python函数时,是不需要指定返回值数据类型的,这和Python不关心变量的数据类型是一致 ...
- Python re 模块findall() 函数返回值展现方式详解
findall 函数: 在字符串中找到正则表达式所匹配的所有子串,并返回一个列表,如果没有找到匹配的,则返回空列表. 注意: match 和 search 是匹配一次 findall 匹配所有,mat ...
- c++11之获取模板函数的参数个数和函数返回值类型
本文演示c++需要支持c++11及以上标准 获取参数个数 1.模板函数声明 template <class R, class... Args> R getRetValue(R(*)(Arg ...
- python使用threading获取线程函数返回值的实现方法
python使用threading获取线程函数返回值的实现方法 这篇文章主要介绍了python使用threading获取线程函数返回值的实现方法,需要的朋友可以参考下 threading用于提供线程相 ...
- Python第七天 函数 函数参数 函数里的变量 函数返回值 多类型传值 函数递归调用 匿名函数 内置函数
Python第七天 函数 函数参数 函数里的变量 函数返回值 多类型传值 函数递归调用 匿名函数 内置函数 目录 Pycharm使用技巧(转载) Python第一天 ...
- Python return语句 函数返回值
return语句是从python 函数返回一个值,在讲到定义函数的时候有讲过,每个函数都要有一个返回值.Python中的return语句有什么作用,今天就来仔细的讲解一下. python 函数返回值 ...
- python函数返回值
2016-08-09 15:01:38 python函数返回值使用return语句,可以返回任意类型的数.如果return语句执行,它之后的所有语句都不再执行. def func(x,y): pri ...
随机推荐
- 用Android Studio 出现的问题
解决的办法是cmd--恢复网络设置---netsh winsock reset----重启电脑解决.
- 去除手机浏览器input焦点默认边框(直接用outline:none就可以了)
1.使用Chrome的都知道,当鼠标焦点在input.textarea这些元素上时,Chrome默认的会给它们加上黄色的边框,我以前一直以为这是chrome的特性,没法去掉,原来是css的效果,out ...
- Day25_多线程第二天
1.单例); } 4.线程之间的通信(多个线程共享同一数据的问题) ; public void print1() throws InterruptedException { synchronized( ...
- intent打开第三方应用
有时候我们会有在自己的应用中进入另一个第三方应用的需求,首先要知道第三方应用的包名和主activity,很简单遍历一下所有的应用就能拿到了. private void go2App(String pa ...
- Ported my old SPH solver in Unity
Here is the link to the web player version, http://www-scf.usc.edu/~taian/pages/sph/builds/12212014/ ...
- AppStore遭遇大BUG
用AppLoader上传,提示这个 The u option must have a non-empty value.The password must have a non-empty value. ...
- yii gii自动生成的curd添加批量删除实例
1.在视图中 CGridView中的columns添加,作用是添加多选框 代码如下 复制代码 array( 'selectableRows' => 2, 'foot ...
- Enhanced Mitigation Experience Toolkit 软件安全性强化工具
Enhanced Mitigation Experience Toolkit软件是微软为应对互联网中层出不穷的漏洞而推出的一款安全工具,可以在Window Update未获取到补丁前,对系统进行保护. ...
- java内存分配--引用
栈内存 对象地址 堆内存 存放属性 public class TestDemo{ public static void main(String args[]){ Person perA =new ...
- 前端学习 第六弹: javascript中的函数与闭包
前端学习 第六弹: javascript中的函数与闭包 当function里嵌套function时,内部的function可以访问外部function里的变量 function foo(x) { ...