Python 并行数组生成

Python parallel array generation

我想生成并行 2 两个数组,i。 e. x1x2 用于进一步分析(比较 x1 中的最大值元素与 x2 中的最大值元素)。 程序的开头是这样的

import multiprocessing
import numpy as np


while true:

   x1 = np.random.normal(0, 500, 12500)
   x2 = np.random.normal(0, 500, 12500)

   if(x1...x2): #condition comparing elements from array x1 and x2
      break

print('Number found)'

我试着重写成这样

import multiprocessing
import numpy as np

def gen():

   np.random.normal(0, 500, 12500)

while true:

   x1 = multiprocessing.Process(target=gen, arg=())
   x2 = multiprocessing.Process(target=gen, arg=())

   x1.start()
   x2.start()

   x1.join()
   x2.join()

   if(x1...x2): #condition comparing elements from array x1 and x2
      break

print('Number found)'

但程序不会生成随机数组 x1, x2 parallel。谢谢你的帮助。

x1x2 在你的第二个示例进程中,你需要实现一个队列 像这样的东西可以工作

import multiprocessing
import numpy as np

def gen(q):
   x = np.random.normal(0, 500, 12500)
   q.put(x)

while true:
   q = multiprocessing.Queue()
   x1 = multiprocessing.Process(target=gen, arg=(q,))
   x2 = multiprocessing.Process(target=gen, arg=(q,))

   x1.start()
   x2.start()
   y1 = q.get()
   y2 = q.get()
   x1.join()
   x2.join()
   if(y1...y2): #condition comparing elements from array y1 and y2
      break