Python:用线程分配总和 [英] Python: Splitting up a sum with threads

查看:106
本文介绍了Python:用线程分配总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要进行昂贵的计算才能拟合一些实验数据.拟合函数是本征模的和,每个本征模都包含一个特定的表面积分.因为如果您按照我考虑的经典方式进行处理,它的运行速度会相当慢.我正在使用python btw.

我要计算的函数类似

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆