为什么运行2个线程会使python崩溃?

2024-10-02 00:44:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在制作一个小程序来计算和绘制函数的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()调用之前停止。在


Tags: lambdaintestimportselffordefpi
1条回答
网友
1楼 · 发布于 2024-10-02 00:44:31

尝试使用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)

相关问题 更多 >

    热门问题