python3中的多进程不能并行工作

python3中的多进程不能并行工作,python,multithreading,python-3.x,multiprocessing,python-multiprocessing,Python,Multithreading,Python 3.x,Multiprocessing,Python Multiprocessing,我想使用python3并行运行一个进程。我的代码一个接一个地运行。有没有关于如何使之平行的想法 from multiprocessing import Process def work(x, outfile): for i in range(0,200000): print(x, i,'hello world', outfile) if __name__ == '__main__': NUM_THREADS = 4 for x in range(NUM

我想使用python3并行运行一个进程。我的代码一个接一个地运行。有没有关于如何使之平行的想法

from multiprocessing import Process

def work(x, outfile):
    for i in range(0,200000):
        print(x, i,'hello world', outfile)

if __name__ == '__main__':
    NUM_THREADS = 4
    for x in range(NUM_THREADS):
        try:
            outfile = "tmp"+str(x) 
            p = Process(target=work, args =(x, outfile)) 
            p.start()
            p.join()
        except:
            raise
            print("Error: unable to start thread", x)

您需要将进程作为守护进程运行

尝试在
p.start()之前添加
p.daemon=True


p.join()
等待进程完成。你也需要摆脱它

你不能在同一个循环的同一块中启动和加入。连接意味着当前正在运行的线程必须停止,直到“进程”启动完成

if __name__ == '__main__':
    NUM_THREADS = 4
    process_list = []
    for x in range(NUM_THREADS):
        try:
            outfile = "tmp"+str(x) 
            p = Process(target=work, args =(x, outfile)) 
            p.start()
            process_list.append(p)
        except:
            raise
            print("Error: unable to start thread", x)

     # wait for processes to finish
     for process in process_list:
         process.join()

我不确定这是否与您相关,但我通常在使用多进程模块时遇到困难,而在使用pathos模块时取得了更大的成功(至少在Linux和Mac中,而不是在Windows中)。我已将其设置为多核使用,但请检查pathos模块的线程/核心拆分使用情况

要感谢Mike McKerns编写了这个包,它使我的生活更容易在python中使用多核

所需代码最少,请参见以下内容:

from pathos.helpers import mp
import numpy as np

x=np.arange(0,200000)
splitx=np.array_split(x,4)
def dummy(y):
    return(y)

pooler=mp.Pool(4)

for value in pooler.imap(dummy,splitx):
    print(value)

pooler.close()
pooler.join()

[    0     1     2 ..., 49997 49998 49999]
[50000 50001 50002 ..., 99997 99998 99999]
[100000 100001 100002 ..., 149997 149998 149999]
[150000 150001 150002 ..., 199997 199998 199999]

首先,sincs multiprocessing.Process将在一个新的python输入程序中运行其目标函数,内置的print dosn不会打印到控制台,以解决这个问题

import jpe_types.paralel
它将覆盖打印状态
但是,您必须使用jpe_types.paralel.Process,而不是multiprocessing.Process来显示进程内打印解释器

此外,您必须启动所有进程,然后在以后加入它们,所以请将它们保存到如下列表中

import jpe_types.paralel

def work(x, outfile):
    for i in range(0,5):
        print(x, i,'hello world', outfile)

if __name__ == '__main__':
    NUM_PROCESSES = 4
    processes = []
    for x in range(NUM_PROCESSES):

        outfile = "tmp"+str(x) 
        p = jpe_types.paralel.Process(target=work, args =(x, outfile)) 
        p.start()
        processes.append(p)
        
    for p in processes:
        p.join()
这比产出更重要

1 0 hello world tmp1
2 0 hello world tmp2
0 0 hello world tmp0
3 0 hello world tmp3
1 1 hello world tmp1
2 1 hello world tmp2
0 1 hello world tmp0
3 1 hello world tmp3
1 2 hello world tmp1
2 2 hello world tmp2
0 2 hello world tmp0
3 2 hello world tmp3
1 3 hello world tmp1
2 3 hello world tmp2
0 3 hello world tmp0
3 3 hello world tmp3
1 4 hello world tmp1
2 4 hello world tmp2
0 4 hello world tmp0
3 4 hello world tmp3
```

p.join()
等待进程完成。你想把它排除在循环之外。将
raise
放在
print
之前意味着你永远不会
print
(重新
raise
绕过后续代码,直到在其他地方被捕获,或者它完全从
\uu main\uuuuu
冒出)。如果
打印对您很重要,您可能需要交换它们。这是错误的,通过:
[守护进程]不是Unix守护进程或服务,它们是正常进程,如果[其父进程]退出,它们将被终止(且不会加入)。
我试图添加
p.Daemon=True
,这似乎没有什么区别。去掉
p.join()
会让事情变得更糟,函数只部分运行了1次,然后就中断并停止了。这个“助手”模块中没有任何东西不适合现有的
多处理.Pool
API;重点是什么?嗯,老实说,我不是这里的专家,但通过包含dill模块,它克服了在多进程模块中酸洗对象类型的一些问题(而且像我这样的Noobie不用担心的问题更少)()