Python child process in while True loop blocks parent

Barry barry at barrys-emacs.org
Mon Nov 29 17:14:36 EST 2021



> On 29 Nov 2021, at 20:36, Jen Kris via Python-list <python-list at python.org> wrote:
> 
> I have a C program that forks to create a child process and uses execv to call a Python program.  The Python program communicates with the parent process (in C) through a FIFO pipe monitored with epoll().  
> 
> The Python child process is in a while True loop, which is intended to keep it running while the parent process proceeds, and perform functions for the C program only at intervals when the parent sends data to the child -- similar to a daemon process. 
> 
> The C process writes to its end of the pipe and the child process reads it, but then the child process continues to loop, thereby blocking the parent. 
> 
> This is the Python code:
> 
> #!/usr/bin/python3
> import os
> import select
> 
> #Open the named pipes
> pr = os.open('/tmp/Pipe_01', os.O_RDWR)
> pw = os.open('/tmp/Pipe_02', os.O_RDWR)

You will need to set the fd’s to non blocking on parent and child.
Otherwise the parent will block on its write until the child reads the message.

Barry
> 
> ep = select.epoll(-1)
> ep.register(pr, select.EPOLLIN)
> 
> while True:
> 
>     events = ep.poll(timeout=2.5, maxevents=-1)
>     #events = ep.poll(timeout=None, maxevents=-1)
> 
>     print("child is looping")
> 
>     for fileno, event in events:
>         print("Python fileno")
>         print(fileno)
>         print("Python event")
>         print(event)
>         v = os.read(pr,64)
>         print("Pipe value")
>         print(v)
> 
> The child process correctly receives the signal from ep.poll and correctly reads the data in the pipe, but then it continues looping.  For example, when I put in a timeout:
> 
> child is looping
> Python fileno
> 4
> Python event
> 1
> Pipe value
> b'10\x00'
> child is looping
> child is looping
> 
> That suggests that a while True loop is not the right thing to do in this case.  My question is, what type of process loop is best for this situation?  The multiprocessing, asyncio and subprocess libraries are very extensive, and it would help if someone could suggest the best alternative for what I am doing here. 
> 
> Thanks very much for any ideas. 
> 
> 
> -- 
> https://mail.python.org/mailman/listinfo/python-list



More information about the Python-list mailing list