1
tqknight OP 求大佬指点
|
2
fanhaipeng0403 2019-01-06 15:40:10 +08:00
Note that with asynchronous programming you don't need to manually deal with result queues - apply_async returns a AsyncResult instance which can be used to get the result: result.get(). This uses an underlying result (out-) queue and so you simply need to return in your target function. Also if you use result.get() and you passed a Queue instance as an argument to the target function it will raise a RuntimeError
|
3
fanhaipeng0403 2019-01-06 15:40:15 +08:00
|
4
bihuchao 2019-01-06 16:23:54 +08:00
multiprocessing 中的 Queue 不能被序列化。
目的应该是这样吧 ``` Python #! /usr/bin/python3 import time import random import multiprocessing queue = multiprocessing.Queue() def perform_task(id): print("{0}# process start".format(id)) begin = time.time() time.sleep(random.random()*5) print("{0}# process end".format(id)) res = "{0}# process time : {1}".format(id, time.time()-begin) print(res) queue.put(res) return if __name__ == "__main__": poolCount = 5 pool = multiprocessing.Pool(poolCount) for i in range(poolCount): pool.apply_async(perform_task, args=(i, )) pool.close() pool.join() print("End tasking, print results:") while True: res = queue.get() print(res) if queue.empty(): break ``` 执行结果: ``` 0# process start 1# process start 2# process start 3# process start 4# process start 0# process end 0# process time : 0.5990872383117676 1# process end 1# process time : 2.662280559539795 3# process end 3# process time : 3.903242826461792 2# process end 2# process time : 4.440236330032349 4# process end 4# process time : 4.543649435043335 End tasking, print results: 0# process time : 0.5990872383117676 1# process time : 2.662280559539795 3# process time : 3.903242826461792 2# process time : 4.440236330032349 4# process time : 4.543649435043335 ``` |
5
zn 2019-01-06 16:31:08 +08:00 via iPhone
为啥我看到函数名就直觉是蟒蛇呢?我基本没怎么学过蟒蛇,同时百分百确定没用过这个函数。
|