Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python fork(): passing data from child to parent

I have a main Python process, and a bunch or workers created by the main process using os.fork().

I need to pass large and fairly involved data structures from the workers back to the main process. What existing libraries would you recommend for that?

The data structures are a mix of lists, dictionaries, numpy arrays, custom classes (which I can tweak) and multi-layer combinations of the above.

Disk I/O should be avoided. If I could also avoid creating copies of the data -- for example by having some kind of shared-memory solution -- that would be nice too, but is not a hard constraint.

For the purposes of this question, it is mandatory that the workers are created using os.fork(), or a wrapper thereof that would clone the master process's address space.

This only needs to work on Linux.

like image 893
NPE Avatar asked Jun 03 '11 12:06

NPE


People also ask

What is a child process in Python?

In Python, we have something known as Child Process. Now, what it is? Child Process is a process that contains complete data as that of the Parent Process which is the main process. The Child Process is just a copy of the Parent process.

How to debug a fork in Python?

Each thread has its own unique ID, and the threads of the same process share the process instructions and the data. One can use pdb on the main process and winpdb on the fork to debug a fork.pdb is the standard library for debugging winpdb is an advanced debugger available in python which supports multiple threading and breakpoints.

How to add threading or forking in Python?

To add threading or forking, a new class can be created using the appropriate mix-in form. os.kill () is the method available in Python which is used to send signal to the specified process with the given process id. In this article, we learned all about forking in python.

What is a child process in Linux?

Child Process is a process that contains complete data as that of the Parent Process which is the main process. The Child Process is just a copy of the Parent process. It copies local variables of Parent Process and any changes done in the Parent Process do not appear in the Child Process. Each process runs until it finishes and then exit.


1 Answers

multiprocessing's queue implementation works. Internally, it pickles data to a pipe.

q = multiprocessing.Queue()
if (os.fork() == 0):
    print(q.get())
else:
    q.put(5)
# outputs: 5
like image 175
Spferical Avatar answered Sep 29 '22 15:09

Spferical