accepting receiving 'spam\ \nreceiving 'eggs\ \nreceiving 'ham\nin other wordseven when line buffering is requestedsocket wrapper file writes (and by associationprintsare buffered until the program exitsmanual flushes are requestedor the buffer becomes full solutions the short story here is thisto avoid delayed outputs or deadlockscripts that might send data to waiting programs by printing to wrapped sockets (or for that matterby using print or sys stdout write in generalshould do one of the followingcall sys stdout flush periodically to flush their printed output so it becomes available as producedas shown in example - be run with the - python command-line flagif possibleto force the output stream to be unbuffered this works for unmodified programs spawned by pipe tools such as os popen it will not help with the use case herethoughbecause we manually reset the stream files to buffered text socket wrappers after process starts to prove thisuncomment example - ' manual flush calls and the sleep call at its endand run with -uthe first test' output is still delayed for seconds use threads to read from sockets to avoid blockingespecially if the receiving program is gui and it cannot depend upon the client to flush see for pointers this doesn' really fix the problem--the spawned reader thread may be blocked or deadlockedtoo--but at least the gui remains active during waits implement their own custom socket wrapper objects which intercept text write callsencode to binaryand route to socket with send callssocket makefile is really just convenience tooland we can always code wrapper of our own for more specific roles for hintssee ' guioutput classthe stream redirection class in and the classes of the io standard library module (upon which python' input/output tools are basedand which you can mix in custom waysskip print altogether and communicate directly with the native interfaces of ipc devicessuch as socket objectsraw send and recv methods--these transfer data immediately and do not buffer data as file methods can we can either transfer simple byte strings this way or use the pickle module' dumps and loads tools to convert python objects to and from byte strings for such direct socket transfer (more on pickle in making sockets look like files and streams