0

我正在制作一个小程序来评估和绘制函数的线性规范变换

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() 之前停止。

4

1 回答 1

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)
于 2012-09-15T10:44:37.990 回答