Python multiprocessing with unrelated processes
I have many proce开发者_如何学Pythonsses that are spawned separately, not from parent to child. The processes need to send a message to specific processes. The receiving processes address (pid) can be stored in a database, but the processes cannot share any common variables in memory.
I could not find any way to accomplish this with pythons multiprocess package and am now looking into a socket-based server, but this issue still left me curious if this kind of architecture could be achieved with multiprocessing - the advantage would be to easily pass pickable objects.
The processes need to send a message to specific processes. The receiving processes address (pid) can be stored in a database, but the processes cannot share any common variables in memory.
Database? Why? Everyone uses a file for this, since a file is cheap, available, and you're only storing one integer value.
Also. Since you're going to use a file, you have more interesting choices.
Every process writes the message to a named pipe. The receiving process takes requests off the named pipe.
Every process writes the message to a file. A simple lock assures that only one process at a time has access to the file, assuring serialization. The receiving process reads from this file.
Every process uses HTTP to make a RESTful request to the receiving process. The receiving process uses a stripped-down HTTP server framework to process requests.
Every process uses a message queue to enqueue messages. The receiving process dequeues the messages. The queue is a file.
etc. And yes, there are more. But they start to get OS-specific.
精彩评论