see "more on stream bufferingpty and pexpecton page thread can avoid blocking main guitoobut really just delegate the problem (the spawned thread will still be deadlockedof the options listedthe first two--manual flushes and command-line arguments--are often the simplest solutions in factbecause it is so usefulthe second technique listed above merits few more words try thiscomment-out all the sys stdout flush calls in examples - and - (the files pipes py and pipes-testchild pyand change the parent' spawn call in pipes py to this ( add - command-line argument)spawn('python''- ''pipes-testchild py''spam'then start the program with command line like thispython - pipes py it will work as it did with the manual stdout flush callsbecause stdout will be operating in unbuffered mode in both parent and child we'll revisit the effects of unbuffered output streams in where we'll code simple gui that displays the output of non-gui program by reading it over both nonblocking socket and pipe in thread we'll explore the topic again in more depth in where we will redirect standard streams to sockets in more general ways deadlock in generalthoughis bigger problem than we have space to address fully here on the other handif you know enough that you want to do ipc in pythonyou're probably already veteran of the deadlock wars anonymous pipes allow related tasks to communicate but are not directly suited for independently launched programs to allow the latter group to conversewe need to move on to the next section and explore devices that have broader visibility more on stream bufferingpty and pexpect on unix-like platformsyou may also be able to use the python pty standard library module to force another program' standard output to be unbufferedespecially if it' not python program and you cannot change its code technicallydefault buffering for stdout in other programs is determined outside python by whether the underlying file descriptor refers to terminal this occurs in the stdio file system library and cannot be controlled by the spawning program in generaloutput to terminals is line bufferedand output to nonterminals (including filespipesand socketsis fully buffered this policy is used for efficiency files and streams created within python script follow the same defaultsbut you can specify buffering policies in python' file creation tools the pty module essentially fools the spawned program into thinking it is connected to terminal so that only one line is buffered for stdout the net effect is that each newline flushes the prior line--typical of interactive programsand what you need if you wish to grab each piece of the printed output as it is produced notehoweverthat the pty module is not required for this role when spawning python scripts with pipessimply use the - python command-line flagpass line-buffered mode interprocess communication