简体   繁体   English

Multiprocessing.Process 没有启动而没有任何错误

[英]Multiprocessing.Process not starting without any error

Below is my demo:下面是我的演示:

Thread xxx:
    import xxx
    import xxxx

    def test_process1(hostname,proxy_param):
        # just never run
        try:                             # breakpoint 0
            with open("/xxx","w+") as f: # breakpoint 1
                f.write("something")
        except Exception as e:
            pass # just never run  breakpoint 3
    def test():
        try:
            a = Process(target=test_process1, args=(hostname,proxy_param))
            a.start()
            a.join() # you are blocking here. test_process1 not working and never quit
        except Exception as e: 
            pass # breakpoint 4

function test_process1 just never run. function test_process1从未运行。 No error, No breakpoint.没有错误,没有断点。

The test function code is in a big project, here is a demo.测试function代码在一个大项目中,这里有一个demo。

Hope.希望。 this piece of code helps.这段代码有帮助。

Workers list will get divided based on the number of processes in use.工人列表将根据正在使用的进程数进行划分。

Sample Code with ManagerList.带有 ManagerList 的示例代码。

from subprocess import PIPE, Popen
from multiprocessing import Pool,Pipe
from multiprocessing import Process, Queue, Manager

def child_process(child_conn,output_list,messenger):
    input_recvd = messenger["input"]
    output_list.append(input_recvd)
    print(input_recvd)
    child_conn.close()

def parent_process(number_of_process=2):
    workers_inputs = [{"input":"hello"}, {"input":"world"}]
    with Manager() as manager:
        processes        = []
        output_list      = manager.list()  # <-- can be shared between processes.
        parent_conn, child_conn = Pipe()
        for single_id_dict in workers_inputs:
            pro_obj             = Process(target=child_process, args=(child_conn,output_list,single_id_dict))  # Passing the list
            pro_obj.start()
            processes.append(pro_obj)
        for p in processes:
            p.join()
        output_list = [single_feature for single_feature in output_list]
    return output_list

parent_process()

OUTPUT: OUTPUT:

hello

world

['hello', 'world']

ManagerList is useful to get the output from various parallel process it's like an inbuilt Queue Mechanism with easy to use and safe from deadlocks. ManagerList 对于从各种并行进程中获取 output 很有用,它就像一个内置的队列机制,易于使用且不会出现死锁。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM