Duplex communication with pipes - is possible ?

Daniel Dittmar daniel.dittmar at sap.corp
Fri Jun 16 16:33:01 CEST 2006

Dara Durum wrote:
> Hi !
> I want to create a Process Pool Object.
> I can hold started processes, and can communicate with them.
> I tryed with many ipc methods, but every of them have bug or other problem.
> Sockets are unavailabe (because Windows Firewall hold them).
> I think I will use pipe.
> The object's pseudocode:
> while not Quit:
> CheckProcessOutputs;
> ProcessReceivedData;
> SendDataToSubProcesses;
> if NoMoreData: Quit=1
> If I used pipes and subprocess module in Windows, I got big freezes,
> and deadlocks.
> Main proc:
> subprocpipe.write('aaaa\n')
> subprocpipe.readlines()
> Sub proc:
> input=sys.stdin.readlines().strip()
> print input+'!!!'
> It is working. But when I move this client code to cycle, I got deadlock.
> while not Quit:
> input=sys.stdin.readlines().strip()
> print input+'!!!'
> Quit=input=='q'
> Why ? Why I cannot create cyclic communication with client ?
> Subprocess must "staying alive" (don't die), and need to stay in
> reuseable state ?
> Simply: subprocess pool needed !!!
> Thanks for help:
> dd

readlines () will try to read until the stream/socket is closed. Try to 
read only one line. This of course means that you cannot sent \n as part 
of the data, you have to escape them somehow.

I'm not sure how the pipe code is searching for the \n. Trying to read 
too much could lead to deadlocks as well. (Although I'm sure that the 
code is written to return fewerbytes than requested if there isn't 
enough data pending in the pipe). A safer variant might be to transfer a 
fixed number of bytes containing the length n of the following data, and 
then n bytes containing the actual data.


More information about the Python-list mailing list