多处理中不同工作人员的相同输出 [英] Same output in different workers in multiprocessing

查看:19
本文介绍了多处理中不同工作人员的相同输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些非常简单的案例,可以将要完成的工作分解并在工人之间分配.我从这里尝试了一个非常简单的多处理示例:

I have very simple cases where the work to be done can be broken up and distributed among workers. I tried a very simple multiprocessing example from here:

import multiprocessing
import numpy as np
import time

def do_calculation(data):
    rand=np.random.randint(10)
    print data, rand
    time.sleep(rand)
    return data * 2

if __name__ == '__main__':
    pool_size = multiprocessing.cpu_count() * 2
    pool = multiprocessing.Pool(processes=pool_size)

    inputs = list(range(10))
    print 'Input   :', inputs

    pool_outputs = pool.map(do_calculation, inputs)
    print 'Pool    :', pool_outputs

上述程序产生以下输出:

The above program produces the following output :

Input   : [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
0 7
1 7
2 7
5 7
3 7
4 7
6 7
7 7
8 6
9 6
Pool    : [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

为什么打印相同的随机数?(我的机器中有 4 个 CPU).这是最好/最简单的方法吗?

Why is the same random number getting printed? (I have 4 cpus in my machine). Is this the best/simplest way to go ahead?

推荐答案

我认为您需要使用 numpy.random.seed 在您的 do_calculation 函数中.

I think you'll need to re-seed the random number generator using numpy.random.seed in your do_calculation function.

我的猜测是随机数生成器 (RNG) 在您导入模块时被植入.然后,当您使用多处理时,您会在 RNG 已经种子的情况下分叉当前进程——因此,您的所有进程都共享相同的 RNG 种子值,因此它们将生成相同的数字序列.

My guess is that the random number generator (RNG) gets seeded when you import the module. Then, when you use multiprocessing, you fork the current process with the RNG already seeded -- Thus, all your processes are sharing the same seed value for the RNG and so they'll generate the same sequences of numbers.

例如:

def do_calculation(data):
    np.random.seed()
    rand=np.random.randint(10)
    print data, rand
    return data * 2

这篇关于多处理中不同工作人员的相同输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆