Tho*_*mas 10 multiprocessing python-3.x
我正在尝试为py3.3探索Python的多处理库,我注意到map_async函数中有一个奇怪的结果,我一直无法解释.我一直期待从回调中存储的结果"乱序".也就是说,如果我将多个任务提供给工作进程,则有些任务应该先于其他任务完成,而不一定按照它们被输入或存在于输入列表中的相同顺序.但是,我得到了一组有序的结果,与输入的任务完全一致.即使在故意试图通过减慢某些过程来"破坏"某些过程(这可能会让其他人在它之前完成)之后就是这种情况.
我在calculate函数中有一个print语句,显示它们是按顺序计算的,但结果仍然是有序的.虽然我不确定我是否可以相信印刷声明是一个很好的指标,事实上实际上是在计算乱序.
测试过程(一般示例):构建一个对象列表,每个对象都包含一个整数.将该对象列表作为参数提交给map_async,以及更新对象的numValue属性的函数"calculate",其值为平方值.然后"calculate"函数返回具有更新值的对象.
一些代码:
import time
import multiprocessing
import random
class NumberHolder():
def __init__(self,numValue):
self.numValue = numValue #Only one attribute
def calculate(obj):
if random.random() >= 0.5:
startTime = time.time()
timeWaster = [random.random() for x in range(5000000)] #Waste time.
endTime = time.time() #Establish end time
print("%d object got stuck in here for %f seconds"%(obj.numValue,endTime-startTime))
#Main Process
if __name__ == '__main__':
numbersToSquare = [x for x in range(0,100)] #I'm
taskList = []
for eachNumber in numbersToSquare:
taskList.append(NumberHolder(eachNumber)) #Create a list of objects whose numValue is equal to the numbers we want to square
results = [] #Where the results will be stored
pool = multiprocessing.Pool(processes=(multiprocessing.cpu_count() - 1)) #Don't use all my processing power.
r = pool.map_async(calculate, taskList, callback=results.append) #Using fxn "calculate", feed taskList, and values stored in "results" list
r.wait() # Wait on the results from the map_async
results = results[0] #All of the entries only exist in the first offset
for eachObject in results: #Loop through them and show them
print(eachObject.numValue) #If they calc'd "out of order", I'd expect append out of order
Run Code Online (Sandbox Code Playgroud)
我发现这个写得很好的响应,似乎支持map_async可能有"乱序"结果的想法:multiprocessing.Pool:何时使用apply,apply_async或map?.我也在这里查看了文档(http://docs.python.org/3.3/library/multiprocessing.html).对于map_async,它说明了这个方法"...如果指定了回调,那么它应该是一个可接受单个参数的可调用.当结果准备就绪时,将对其应用回调(除非调用失败).回调应该立即完成,否则处理结果的线程将被阻止"
我误解了它应该如何工作?任何帮助深表感谢.
Ble*_*der 12
这是预期的行为.文档说:
map()返回结果对象的方法的变体.
"结果对象"只是一个容纳计算结果的容器类.当您致电时r.wait(),请等待所有结果汇总并按顺序排列.即使它不按顺序处理任务,结果仍将按原始顺序排列.
如果您希望在计算结果时产生结果,请使用imap_unordered.
| 归档时间: |
|
| 查看次数: |
9263 次 |
| 最近记录: |