Okay, so I’ve been messing around with this thing called “emiru parent”. I had this idea in my head, and I just had to try it out. I wanted to build a parent-child relationship using Python, specifically focusing on how processes could talk to each other.
First, I created a simple Python script. My goal was to have a main process (the parent) that could spawn a child process and then they’d kinda… chat.
Getting Started
I started by importing the multiprocessing module. That’s the key to making this whole thing work. It lets you create new processes super easily.
import multiprocessing
Then, I defined a function for the child process. This is what the child process will actually do.
def child_task(conn):
message = *()
print(f"Child received: {message}")
*("Hello from the child!")
See that conn thing? That’s a connection object. It’s how the parent and child will send messages back and forth. I used the recv() method to get a message and the send() method to send one back.
Building the Parent
Next, I needed to set up the parent process. This part was a little trickier, but not too bad.
I used , and this gives you two connection objects – one for the parent and one for the child, very convenient! Then, I created the child process using . The target is the function the child will run (child_task), and args is how I passed that connection object to the child.
Making Them Talk
Now for the fun part – actually making them communicate!
child_*()
parent_*("Hello from the parent!")
response = parent_*()
print(f"Parent received: {response}")
child_*()
I started the child process with child_*(). Then, the parent sent a message using parent_*(). I waited for a response with parent_*(), and then I printed it out. Finally, I usedchild_*(). This is super important – it makes the parent process wait for the child process to finish before exiting.
When I ran this, I saw the parent send its message, the child receive it and send its own message back, and then the parent got that response. Boom! Parent-child communication achieved! It’s not super fancy, but it’s a good starting point for more complex stuff.