【问题标题】:Why does running 2 threads crash python?为什么运行 2 个线程会使 python 崩溃?
【发布时间】:2012-09-08 05:12:23
【问题描述】:

我正在制作一个小程序来评估和绘制函数的linear canonical transform:

from scipy import *
from scipy.integrate import *
import time
from threading import *
def lct(f, a, b, c, d):
    def X(u):
        coeff=sqrt(-1j)*e**(1j*pi*(d/b)*(u**2))
        integrand_R= lambda t,f,a,b: (e**(-2j*pi*u*t/b)*e**(1j*pi*a*t**2/b)*f(t)).real
        integrand_I= lambda t,f,a,b: (e**(-2j*pi*u*t/b)*e**(1j*pi*a*t**2/b)*f(t)).imag
        # integral= sum of integrals of real and imaginary parts
        integral=quad(integrand_R,-Inf,0,args=(f,a,b))[0]+1j*quad(integrand_I,-Inf,0,args=(f,a,b))[0]
        #print(integral)
        return coeff*integral
    return X
class Executor(Thread):
    def __init__(self, f):
        self._f = f
        Thread.__init__(self)
    def run(self):
        y=[self._f(x_i) for x_i in x]
    def result():
        return y
#thread pool
class Pool:
    def map(self,f,x):
        executors=[Executor(f) for i in range(1)]
        x=x.reshape(8,-1)
        for i in range(len(executors)):
            executors[i].x=x[i]
            executors[i].start()
            #executors[i].join()
        #raise TypeError
        for e in executors:
            e.join()
        raise TypeError#execution does not make it this far if two threads are used




start=time.clock()

p=Pool()
x=arange(4,step=0.005)
test_lct=lct(lambda x: sin(x),1,2,3,7)
def test():
    y=abs(p.map(test_lct,x))
    raise TypeError
    figure(figsize=(6*3.13,4*3.13/2))
    plot(x,y)
    for i in range(y.size):
        if y[i]>1e15:
            print(x[i])
            print(y[i])
            print('\n')
            print(x[130:140])
            print('\n')
            print(y[130:140])
            print('\n')
test()
test_lct=lct(lambda x: sin(2*x),1,2,3,7)
test()

stop=time.clock()
print(stop-start)

工作应该由线程池分配给 8 个线程,但是如果我将 executors=[Executor(f) for i in range(1)](第 26 行)更改为 executors=[Executor(f) for i in range(2)],Python 崩溃:“python.exe 已停止工作”。 python 为什么两个线程会崩溃?

注意:这可以在没有交互式解释器/matplotlib 的情况下运行,因为它在调用 plot() 之前停止。

【问题讨论】:

  • 可能是 scipy 中的一个错误(有人忘记同步他们的变量或锁定 GIL)...更新到最新版本,或提交错误。 Python 代码通常不会使解释器崩溃,除非你正在做一些非常肮脏的事情(例如使用 ctypes),而且它看起来不像你。
  • 可能想要使用多处理而不是线程作为解决方法(并且无论如何都要避免 GIL)。请注意,quad 与 fortran 库接口。由于它可能是 scipy.integrate.quad 中的 GIL 或类似相关问题,最好创建一个错误报告。

标签: python multithreading python-3.x threadpool linear-algebra


【解决方案1】:

尝试使用multiprocessing.Pool。它通过使用多个进程来避免 GIL。

我没有安装 scipy,所以我无法测试它,但试试这样的。

from scipy import *
from scipy.integrate import *
import time
from multiprocessing import Pool
from matplotlib.pyplot import figure, plot

def lct(f, a, b, c, d):
    def X(u):
        coeff=sqrt(-1j)*e**(1j*pi*(d/b)*(u**2))
        integrand_R= lambda t,f,a,b: (e**(-2j*pi*u*t/b)*e**(1j*pi*a*t**2/b)*f(t)).real 
        integrand_I= lambda t,f,a,b: (e**(-2j*pi*u*t/b)*e**(1j*pi*a*t**2/b)*f(t)).imag 
        # integral= sum of integrals of real and imaginary parts
        integral=quad(integrand_R,-Inf,0,args=(f,a,b))[0]+1j*quad(integrand_I,-Inf,0,args=(f,a,b))[0]
        #print(integral)
        return coeff*integral
    return X

def test():
    global test_lct, x
    y=abs(p.map(test_lct,x))
    figure(figsize=(6*3.13,4*3.13/2))
    plot(x,y)
    for i in range(y.size):
        if y[i]>1e15:
            print(x[i])
            print(y[i])
            print('\n')
            print(x[130:140])
            print('\n')
            print(y[130:140])
            print('\n')

if __name__ == '__main__':
  p=Pool()
  x=arange(4,step=0.005)
  start=time.clock()
  test_lct=lct(lambda x: sin(x),1,2,3,7)
  test()
  test_lct=lct(lambda x: sin(2*x),1,2,3,7)
  test()
  stop=time.clock()
  print(stop-start)

【讨论】:

  • 我试过了,但它似乎一直在启动进程,直到它填满我的记忆。
  • 这很奇怪,因为multiprocessing 通常只启动与核心数量一样多的进程。在创建池时尝试使用maxtasksperchild 参数,在调用map 时尝试使用chunksize 参数。不过,我对 scipy 并不熟悉。它是否有可能退出产卵过程?
  • 如果您在程序的主要部分创建池,则会在 Windows 上发生这种情况,因为子项导入文件(在 Windows 上)。将池创建放在if __name__ == '__main__' 守卫中。
  • @Dougal 感谢您的提醒。我已经编辑了我的答案以反映这一点。
猜你喜欢
  • 1970-01-01
  • 2015-08-23
  • 2017-04-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-25
  • 1970-01-01
相关资源
最近更新 更多