OS: Windows 2003 Server R2 x64 Standard Edition Python: 2.6.5 MSVC: 9.0 Application Description: Windows service calling Python C API to run algorithm written in Python. I've been encountering a random application hang when calling the Python C API function PyObject_CallMethod to call a function on a class instance. I've discovered that the C function fwrite in string_print in stringobject.c is blocking after being called multiple times due to print statements in Python code. My application as a Windows service does not have a stdout and fwrite is buffering data before attempting to write it to a non-existent stdout, thus blocking and hanging the service. This isn't an issue when the application has a console window because stdout is available. We are in the process changing the print statements to write to file. Ideally a Python exception needs raising if print cannot write to stdout.
I've created a test service that demostrates the problem. The example service calls PyRun_SimpleString in loop that evaluates the Python statement "print 'String'". The number of iteration in the loop depends on the length of the string used in the print statement. The longer the string the fewer iteration. Attached is a Visual Studio 2008 project and readme.
Closing as a duplicate of . Instead of changing all print statements, you could set sys.stdout to another file, for example: sys.stdout=StringIO() or another object that simply discards written data.