我想写一个小程序,为我提供彩票中奖的机会。在那之后,我想通过像这样嵌入多处理来让它更快一点
但是两个奇怪的行为开始了
import random as r
from multiprocessing.pool import ThreadPool
# winnerSequence = []
# mCombinations = []
howManyLists = 5
howManyTry = 1000000
combinations = 720/10068347520
possbilesNumConstantsConstant = []
for x in range(1, 50):
possbilesNumConstantsConstant.append(x)
def getTicket():
possbilesNumConstants = list(possbilesNumConstantsConstant)
toReturn = []
possiblesNum = list(possbilesNumConstants)
for x in range(6):
choice = r.choice(possiblesNum)
toReturn.append(choice)
possiblesNum.remove(choice)
toReturn.sort()
return toReturn
def sliceRange(rangeNum,num):
"""returns list of smaller ranges"""
toReturn = []
rest = rangeNum%num
print(rest)
toSlice = rangeNum - rest
print(toSlice)
n = toSlice/num
print(n)
for x in range(num):
toReturn.append((int(n*x),int(n*(x+1)-1)))
print(toReturn,"<---range")
return toReturn
def Job(tupleRange):
"""Job returns list of tickets """
toReturn = list()
print(tupleRange,"Start")
for x in range(int(tupleRange[0]),int(tupleRange[1])):
toReturn.append(getTicket())
print(tupleRange,"End")
return toReturn
result = list()
第一个当我将 Job(tupleRange( 添加到池时,看起来在将另一个作业添加到池之前,作业已在主线程中完成
def start():
"""this fun() starts program"""
#create pool of threads
pool = ThreadPool(processes = howManyLists)
#create list of tuples with smaller piece of range
lista = sliceRange(howManyTry,howManyLists)
#create list for storing job objects
jobList = list()
for tupleRange in lista:
#add job to pool
jobToList = pool.apply_async(Job(tupleRange))
#add retured object to list for future callback
jobList.append(jobToList)
print('Adding to pool',tupleRange)
#for all jobs in list get returned tickes
for job in jobList:
#print(job.get())
result.extend(job.get())
if __name__ == '__main__':
start()
康索尔输出
[(0, 199999), (200000, 399999), (400000, 599999), (600000, 799999), (800000, 999999)] <---range
(0, 199999) Start
(0, 199999) End
Adding to pool (0, 199999)
(200000, 399999) Start
(200000, 399999) End
Adding to pool (200000, 399999)
(400000, 599999) Start
(400000, 599999) End
第二个当我想从线程获取数据时,我在此行上得到了此异常
for job in jobList:
#print(job.get())
result.extend(job.get()) #<---- this line
File "C:/Users/CrazyUrusai/PycharmProjects/TestLotka/main/kopia.py", line 79, in start
result.extend(job.get())
File "C:UsersCrazyUrusaiAppDataLocalProgramsPythonPython36libmultiprocessingpool.py", line 644, in get
raise self._value
File "C:UsersCrazyUrusaiAppDataLocalProgramsPythonPython36libmultiprocessingpool.py", line 119, in worker
result = (True, func(*args, **kwds))
TypeError: 'list' object is not callable
索姆博迪可以向我解释一下吗?(我是多处理新手(
问题就在这里:
jobToList = pool.apply_async(Job(tupleRange))
Job(tupleRange)
首先执行,然后apply_async
获取一些返回值,list
类型(如返回列表Job
(。这里有两个问题:此代码是同步的,并且async_applylist
而不是它期望的作业。因此,它尝试将给定列表作为作业执行,但失败了。
这是pool.apply_async的签名:
def apply_async(self, func, args=(), kwds={}, callback=None,
error_callback=None):
...
因此,应将args
func
和参数分别发送到此函数,并且不应在将函数发送到池之前执行该函数。
我修复了这一行,您的代码对我有用:
jobToList = pool.apply_async(Job, (tupleRange, ))
或者,使用显式命名的参数,
jobToList = pool.apply_async(func=Job, args=(tupleRange, ))
不要忘记将函数参数包装在元组左右。