Python:用线程分配总和 [英] Python: Splitting up a sum with threads
问题描述
我要计算的函数类似
def fit_func(params , Mmin, Mmax):
values = np.zeros(1000)
for m in range(Mmin, Mmax):
# Fancy Calculation for each mode
# some calulation with all modes, adding them up 'values'
return values
我该如何拆分?我做了类似的事情
data1 = thread.start_new_thread(fit_func, (params,0,13))
data2 = thread.start_new_thread(fit_func, (params,13,25))
但是data1和data2的总和与fitfunc(params,0,25)...
尝试multiprocessing
.这将使用类似线程的接口有效地创建单独的Python进程.但是,请确保对计算进行概要分析,并确保是问题所在,而不是IO之类的问题.启动过程非常缓慢,因此,如果您打算使用它们,请让它们保持一段时间.
您也可以将numpy
用于这些功能.它们是用C代码编写的,所以它们很傻.将它们都检查出来,看看最合适的.我自己去找numpy
解决方案...
i have a costly calculation to do for fitting some experimental data. The fitting function is a sum over eigenmodes, each of them containing a specific surface integral. As it is rather slow if you do it the classical way i thought about threading it. I'm using python btw.
The function i want to calculate is something like
def fit_func(params , Mmin, Mmax):
values = np.zeros(1000)
for m in range(Mmin, Mmax):
# Fancy Calculation for each mode
# some calulation with all modes, adding them up 'values'
return values
How can i split this up? I did something like
data1 = thread.start_new_thread(fit_func, (params,0,13))
data2 = thread.start_new_thread(fit_func, (params,13,25))
but then the sum of data1 and data2 is not the same as fitfunc(params, 0,25)...
Try out multiprocessing
. This will effectively create separate Python processes using a thread-like interface. However, make sure that you profile your computation and make sure that it is the problem, not something else like IO. Starting processes is very slow, so keep them around for a while if you are planning to use them.
You can also use numpy
for those functions. They're written in C code, so they're stupid fast. Check them both out and see what fits best. I would go for the numpy
solution myself...
这篇关于Python:用线程分配总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!