定义了一个函数和一个列表,如何让python多线程执行呢,python高手在吗? 告诉我失败原因,如何修改。

#-*-coding:utf-8-*-defPointsTuple2(R,L,ALPHA,M,ANGLE):lst=[]pitch=2.0*np.pi*R/np.tan(... # -*- coding: utf-8 -*-

def PointsTuple2(R,L,ALPHA,M,ANGLE):

lst=[]

pitch=2.0*np.pi*R/np.tan(np.radians(ALPHA))

pitch1=2.0*np.pi*11.5/np.tan(np.radians(7.16))

newM=int(np.ceil(M*L/pitch))

DtaPitch=0

if pitch > pitch1 :

DtaPitch= pitch-pitch1

else:

DtaPitch=0

for i in range(newM):

ii=float(i)

x=R*np.cos(2.0*np.pi*ii/M+np.radians(ANGLE))

y=R*np.sin(2.0*np.pi*ii/M+np.radians(ANGLE))

z=ii*(pitch-DtaPitch)/M

pointCoordinates=(x,y,z)

lst.append(pointCoordinates)

Points=tuple(lst)

print 'PointsNum:',len(Points),'DtaPitch:',DtaPitch,'newM:',newM,'R=%f,pitch=%f'%(R,pitch)

return Points

#

import numpy as np

from multiprocessing.dummy import Pool as ThreadPool

pool = ThreadPool(4)

#

global newM

L1,L2,L3,ALPHA1,M1,newM=912.0,938.0,991.0,7.16,102.0,0.0

dAngle=12.629916088

ddAngle=(36.0-dAngle)/8.0 #意欲将阴线分为8个网格

Points1=PointsTuple2(R=11.5,L=L1,ALPHA=ALPHA1,M=M1,ANGLE=11.685041956)

Points2=PointsTuple2(R=11.5,L=L1,ALPHA=ALPHA1,M=M1,ANGLE=23.954074918)

#对比多线程

P1=[11.5,912.0,7.16,102.0,11.685041956]

P2=[11.5,L1,ALPHA1,M1,23.954074918]

P3=[11.85,L2,ALPHA1,M1,11.685041956]

P4=[11.85,L2,ALPHA1,M1,23.954074918]

list1=[P1,P2,P3,P4]

results9=pool.map(PointsTuple2,list1)

pool.close()

pool.join()
展开
 我来答
和谐且精明丶爱侣e
2015-04-21 · TA获得超过360个赞
知道小有建树答主
回答量:170
采纳率:0%
帮助的人:215万
展开全部
提示错误时因为你通过map的话list取的话是一个P1对象,是一个列表,传到P ointsTuple2的话是一整个列表,但是你定义的函数需要5个参数,才会报那样的错,改的方法有两个,下面这个改参数为tuple,在把参数赋值,或者加个中介,希望有帮助
修改成一个参数:
# -*- coding: utf-8 -*-
def PointsTuple2(tuple1):
R,L,ALPHA,M,ANGLE=tuple1
lst=[]
pitch=2.0*np.pi*R/np.tan(np.radians(ALPHA))
pitch1=2.0*np.pi*11.5/np.tan(np.radians(7.16))
newM=int(np.ceil(M*L/pitch))
DtaPitch=0
if pitch > pitch1 :
DtaPitch= pitch-pitch1
else:
DtaPitch=0
for i in range(newM):
ii=float(i)
x=R*np.cos(2.0*np.pi*ii/M+np.radians(ANGLE))
y=R*np.sin(2.0*np.pi*ii/M+np.radians(ANGLE))
z=ii*(pitch-DtaPitch)/M
pointCoordinates=(x,y,z)
lst.append(pointCoordinates)
Points=tuple(lst)
print 'PointsNum:',len(Points),'DtaPitch:',DtaPitch,'newM:',newM,'R=%f,pitch=%f'%(R,pitch)
return Points
#
import numpy as np
from multiprocessing.dummy import Pool as ThreadPool
pool = ThreadPool(4)
#
global newM,L1,L2,L3,ALPHA1,M1
L1,L2,L3,ALPHA1,M1,newM=912.0,938.0,991.0,7.16,102.0,0.0
dAngle=12.629916088
ddAngle=(36.0-dAngle)/8.0 #意欲将阴线分为8个网格
tuple1=(11.5,L1,ALPHA1,M1,11.685041956)
Points1=PointsTuple2(tuple1)
tuple2=(11.5,L1,ALPHA1,M1,11.685041956)
Points2=PointsTuple2(tuple2)
#对比多线程
P1=(11.5,912.0,7.16,102.0,11.685041956)
P2=(11.5,L1,ALPHA1,M1,23.954074918)
P3=(11.85,L2,ALPHA1,M1,11.685041956)
P4=(11.85,L2,ALPHA1,M1,23.954074918)
list1=[P1,P2,P3,P4]
results9=pool.map(PointsTuple2,list1)
pool.close()
pool.join()
另外这个运行可能有点问题(google的样本,可以借鉴一下他的写法):
from multiprocessing import Pool
import numpy as np
pool = Pool(processes=2) # I like to calculate only 2 FFTs parallel
# in every time step, therefor 2 processes
def Splitter(args):
'''I have to pass 2 arguments'''
return makeSomething(*args)
def makeSomething(a,b):
'''dummy function instead of the one with the FFT'''
return a*b
def RungeK():
# ...
# a lot of code which create the vectors A and B and calculates
# one Kunge-Kutta step for them
# ...
pool = Pool(processes=2)
n = 20 # Just something for the example
A = np.arange(50000)
B = np.ones_like(A)
for i in xrange(n): # loop over the time steps
A *= np.mean(B)*B - A
B *= np.sqrt(A)
results = pool.map(Splitter, [(A, 3), (B, 2)])
A = results[0]
B = results[1]
pool.close()
print np.mean(A) # Some output
print np.max(B)
if __name__== '__main__':
RungeK()
黑板客
推荐于2016-01-07 · TA获得超过469个赞
知道小有建树答主
回答量:193
采纳率:100%
帮助的人:192万
展开全部

这个很简单。你把map的用法搞混了,再定义一个函数

def PointsTuple1(L):
    R,L,ALPHA,M,ANGLE=L
    return PointsTuple2(R,L,ALPHA,M,ANGLE)

然后把results9=pool.map(PointsTuple2,list1)的函数PointsTuple2换成上面的PointsTuple1
就可以了。


推荐你看看云课堂的用Python做些事,里面第四章里map,reduce讲过。

本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式