user3582642
user3582642

Reputation: 41

Returns values of functions Python Multiprocessing.Process

I'm trying to generate the checksum of two identical files (in two different directories) and am using multiprocessing.Process() to run the checksum of both files simultaneously instead of sequentially.

However, when I run the multiprocessing.Process() object on the checksum generating function I get this return value:

<Process(Process-1, stopped)> 
<Process(Process-2, stopped)>

These should be a list of checksum strings.

The return statement from the generating function is:

return chksum_list

Pretty basic and the program works well when running sequentially.

How do I retrieve the return value of the function that is being processed through the multiprocessing.Process() object?

Thanks.

Upvotes: 1

Views: 1332

Answers (1)

jmetz
jmetz

Reputation: 12783

The docs are relatively good on this topic;

Pipes

You could communicate via a pipe to the process objects;

From the docs:

from multiprocessing import Process, Pipe

def f(conn):
    conn.send([42, None, 'hello'])
    conn.close()

if __name__ == '__main__':
    parent_conn, child_conn = Pipe()
    p = Process(target=f, args=(child_conn,))
    p.start()
    print parent_conn.recv()   # prints "[42, None, 'hello']"
    p.join()

Pool & map

Alternatively you could use a Pool of processes:

pool = Pool(processes=4) 
returnvals = pool.map(f, range(10))

where f is your function, which will act on each member of range(10). Similarly, you can pass in any list containing the inputs to your processes;

returnvals = pool.map(f, [input_to_process_1, input_to_process_2])    

In your specific case, input_to_process_1/2 could be paths to the files you're doing checksums on, while f is your checksum function.

Upvotes: 6

Related Questions