First of all, the general problem that I am solving is a bit more complicated than what I am showing here, so please do not tell me to “use threads with blocking”, as this will not solve my real situation without fair, FAIR-bit rewriting and refactoring.
I have several applications that are not mine for change that take data from stdin and push it to stdout after their magic. My task is to link several of these programs. The problem is that sometimes they choke, and so I need to track their progress, which is displayed on STDERR.
pA = subprocess.Popen(CommandA, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
Now, reading directly through pA.stdout.readline () and pB.stdout.readline () or the plain read () function is a blocking problem. Since different applications are displayed in different steps and in different formats, locking is not an option. (And, as I wrote above, streams are not an option if in the final, last resort.) pA.communicate() is safe from the point of view of deadlocks, but since I need live information, this is not an option either.
So google led me to this on ActiveState.
At first everything is fine until I implement it. Comparing the output of cmd.exe pA.exe | pB.exe pA.exe | pB.exe , ignoring the fact that both outputs in the same window create a mess, I see very instant updates. However, I am implementing the same thing using the above snippet and the read_some() function declared there, and it takes more than 10 seconds to notify read_some() of updates to one channel. But when this happens, he has updates that lead to success, for example, up to 40%.
Thus, I do some more research and see numerous topics regarding PeekNamedPipe, anonymous descriptors and returns 0 bytes, even if there is information available in the pipe. Since the subject proved that he far surpasses my experience for fixing or coding, I came to Stack Overflow to find a guide. :)
My W7 platform is 64-bit with Python 2.6, 32-bit applications, if that matters, and Unix compatibility is not a concern. I can even deal with the full ctypes or pywin32 solution, which completely undermines the subprocess if it is the only solution, if I can read from any stderr pipe asynchronously with immediate performance and without deadlocks. :)