我正在制作一个小程序来评估和绘制函数的线性正则变换:
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()之前停止。
尝试使用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)