如果您尝试运行以下代码:
from multiprocessing import Pool
from multiprocessing.pool import ThreadPool
import random
def do_thing(upper):
print(random.randint(0,99))
random.seed(0)
with ThreadPool(1) as pool:
list(pool.imap(do_thing, [99]))
with ThreadPool(1) as pool:
list(pool.imap(do_thing, [99]))
with Pool(1) as pool:
list(pool.imap(do_thing, [99]))
with Pool(1) as pool:
list(pool.imap(do_thing, [99]))
您会发现ThreadPool
s在多个运行中打印一致的整数,但Pool
s没有。我从这里了解到,我们无法保证流程的创建顺序,因此在许多情况下,无法保证一致的结果。但在我的情况下,这种情况可能发生的订单只有这么多,但有很多不同的结果。所以我不认为链接的帖子是在解释这里发生的事情。
注意,我想";传播";种子,不是用相同的号码重新播种的。我不希望输出都一样。
此外,对于一个经理来说,这似乎是可能的,但只是想知道是否有一个更容易的";明显的";我不知道的方式。
解决这个问题的一种方法(我认为唯一实用的方法(是提出一个托管随机数生成器类,您可以将其作为参数(此处选择的选项(传递给辅助函数,或用于将池中的每个进程初始化为全局变量。我稍微修改了您的代码,使函数do_thing
返回值,而不是打印随机数,我还修改了主进程,以创建一个大小为8的池,并调用do_thing
8次。最后,为了确保所有8个处理器每个处理一个提交的任务(我有8个核心(,而不是第一个处理所有8个任务,当提交的作业很快完成时,这是可能的,我添加了对sleep
到do_thing
:的调用
from multiprocessing import Pool, current_process
from multiprocessing.managers import BaseManager
import random
from functools import partial
class RandomGeneratorManager(BaseManager):
pass
class RandomGenerator:
def __init__(self):
random.seed(0)
def get_random(self):
return random.randint(0, 99)
def do_thing(random_generator, upper):
import time
time.sleep(.2)
print(current_process())
return random_generator.get_random()
# Required for Windows:
if __name__ == '__main__':
RandomGeneratorManager.register('RandomGenerator', RandomGenerator)
with RandomGeneratorManager() as manager:
random_generator = manager.RandomGenerator()
# random_generator will be the first argument to do_thing:
worker = partial(do_thing, random_generator)
with Pool(8) as pool:
print(pool.map(worker, [0] * 8))
with Pool(8) as pool:
print(pool.map(worker, [0] * 8))
打印:
<SpawnProcess name='SpawnPoolWorker-3' parent=23956 started daemon>
<SpawnProcess name='SpawnPoolWorker-2' parent=23956 started daemon>
<SpawnProcess name='SpawnPoolWorker-4' parent=23956 started daemon>
<SpawnProcess name='SpawnPoolWorker-5' parent=23956 started daemon>
<SpawnProcess name='SpawnPoolWorker-7' parent=23956 started daemon>
<SpawnProcess name='SpawnPoolWorker-6' parent=23956 started daemon>
<SpawnProcess name='SpawnPoolWorker-9' parent=23956 started daemon>
<SpawnProcess name='SpawnPoolWorker-8' parent=23956 started daemon>
[49, 97, 53, 5, 33, 65, 51, 62]
<SpawnProcess name='SpawnPoolWorker-14' parent=23956 started daemon>
<SpawnProcess name='SpawnPoolWorker-10' parent=23956 started daemon>
<SpawnProcess name='SpawnPoolWorker-13' parent=23956 started daemon>
<SpawnProcess name='SpawnPoolWorker-11' parent=23956 started daemon>
<SpawnProcess name='SpawnPoolWorker-17' parent=23956 started daemon>
<SpawnProcess name='SpawnPoolWorker-15' parent=23956 started daemon>
<SpawnProcess name='SpawnPoolWorker-16' parent=23956 started daemon>
<SpawnProcess name='SpawnPoolWorker-12' parent=23956 started daemon>
[38, 61, 45, 74, 27, 64, 17, 36]