任何参数都不满足的多处理



我有一个数字列表,我想让 2 个不同的 CPU 内核打印每个列表的一半。 一个核心从开头开始,另一个从中间开始。

我已经进行了 2 次尝试。两者都给出错误,告诉我尝试其他尝试 此处的尝试和错误示例

错误 1 = 参数太多,只需要 1

错误 2 = 1 arg 不可迭代

(下定决心(

代码 1(不迭代(

import multiprocessing as mp
import numpy as np
A = np.linspace(0,99999999,999999)
def print_stuff(i):
for j in i:    # i want two seperate proccessores to print half of this list each 
print(A[j]) 
def do_stuff():
print("doing stuff")
kk = range(int(len(A/2)))
kk2 = [] # to use to index places in the lists
for i in kk:
kk2.append(i+kk[-1])
print(len(kk2))
p1 = mp.Process(target = print_stuff, args = kk,)
p2 = mp.Process(target = print_stuff, args = kk2,)
p1.start()
p2.start()
# This will terminate the thread when they done their job(s)
p1.join()
p2.join()
print("done stuff")
if __name__ == "__main__":
do_stuff()

代码 2(迭代(

import multiprocessing as mp
import numpy as np
A = np.linspace(0,99999999,999999)
def print_stuff(i):
# i want two seperate proccessores to print half of this list each 
print(A[j]) 
def do_stuff():
print("doing stuff")
kk = range(int(len(A/2)))
kk2 = []
for i in kk:
kk2.append(i+kk[-1])
print(len(kk2))
for i in range(int(len(A/2))):
p1 = mp.Process(target = print_stuff, args = kk[i],)
p2 = mp.Process(target = print_stuff, args = kk2[i],)
p1.start()
p2.start()
# This will terminate the thread when they done their job(s)
p1.join()
p2.join()
print("done stuff")
if __name__ == "__main__":
do_stuff()

第一个代码有问题,因为您要在需要整数的地方传递一个可迭代对象。

对于第一个代码,您必须记住,多处理进程启动一个新的python解释器,因此打印将不起作用
您可以使用jpe_types.paralel.process来避免此问题。 代码将如下所示

import jpe_types.paralel
import numpy as np
A = np.linspace(0,10,10)
def print_stuff(start):
for j in range(start, len(A), 2):    # i want two seperate proccessores to print half of this list each 
print(A[j]) 
def do_stuff():
p1 = jpe_types.paralel.Process(target=print_stuff, args=(1,))
p2 = jpe_types.paralel.Process(target=print_stuff, args=(0,))
p1.start()
p2.start()
p1.join()
p2.join()
if __name__ == "__main__":
do_stuff()

然而这没有意义,因为 overiden print 语句只是将所有内容放入多处理队列中,并在主模块中有一个线程来打印放入该队列中的任何内容,因此您不会获得任何内容


对于第二个代码
works 文件,如下所示:
但请注意,它仅在 A 长度偶数时才有效

import jpe_types.paralel
import numpy as np
A = np.linspace(0,10,10)
def print_stuff(j):
# i want two seperate proccessores to print half of this list each 
print(A[j]) 
def do_stuff():
print("doing stuff")
kk = range(int(len(A)/2)) # you want half the lentght
kk2 = []
for i in kk:
kk2.append(i+kk[-1])
for i in range(int(len(A/2))):
p1 = jpe_types.paralel.Process(target = print_stuff, args = (kk[i],))
p2 = jpe_types.paralel.Process(target = print_stuff, args = (kk2[i],))
p1.start()
p2.start()
# This will terminate the thread when they done their job(s)
p1.join()
p2.join()
print("done stuff")
if __name__ == "__main__":
do_stuff()

顺便说一句,这段代码比仅仅慢得多

for e in A:
print(e)

因为创建流程
hope 可以回答您的问题,因此涉及大量开销

相关内容

  • 没有找到相关文章