Title: print statement hangs Windows service
Type: behavior Stage:
Components: Interpreter Core, Windows Versions: Python 2.6
Status: closed Resolution: duplicate
Dependencies: Superseder: print in pythonw raises silent exception when no console available
View: 706263
Assigned To: Nosy List: amaury.forgeotdarc, eric.smith, rgpitts, tim.golden
Priority: normal Keywords:

Created on 2010-08-04 07:34 by rgpitts, last changed 2010-09-10 06:49 by amaury.forgeotdarc. This issue is now closed.

File name Uploaded Description Edit rgpitts, 2010-08-04 10:20 Visual Studio 2008 project and readme
Messages (3)
msg112768 - (view) Author: rgpitts (rgpitts) Date: 2010-08-04 07:34
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.
msg112784 - (view) Author: rgpitts (rgpitts) Date: 2010-08-04 10:20
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.
msg115982 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2010-09-10 06:49
Closing as a duplicate of issue706263.

Instead of changing all print statements, you could set sys.stdout to another file, for example:
or another object that simply discards written data.
Date User Action Args
2010-09-10 06:49:24amaury.forgeotdarcsetstatus: open -> closed

nosy: + amaury.forgeotdarc
messages: + msg115982

superseder: print in pythonw raises silent exception when no console available
resolution: duplicate
2010-08-10 11:42:59floxsetnosy: eric.smith, tim.golden, rgpitts
components: + Windows
2010-08-04 10:20:32rgpittssetfiles: +

messages: + msg112784
2010-08-04 09:40:46eric.smithsetnosy: + eric.smith
2010-08-04 08:38:21tim.goldensetnosy: + tim.golden
2010-08-04 07:34:14rgpittscreate