Pythonw.exe exits prematurely

Discussion in 'Python' started by Brainsludge, Oct 23, 2007.

  1. Brainsludge

    Brainsludge Guest


    I am running WinXP SP2 with Python 2.5.1 and encountered the following

    I wrote a script that logs into my mail server, and checks for new
    messages every 20 seconds. When a new message is found, it displays a
    Windows tool tip and prints a line to console indicating a new

    When I run the script with python.exe (console), the script runs fine
    and checks for new messages in perpetuity (as it should). However,
    when I run the script with pythonw.exe (no console), the script
    prematurely exits after 10 or 15 minutes. For those 15 minutes, it
    works as it should, displaying tool tips when new messages arrive.
    Furthermore, when it exits prematurely, it appears to be exiting
    gracefully (I have an exit routine that logs out of the mail server
    and cleans up).

    Does anyone know why this might be the case? The lines printed to
    console are purely for diagnostic purposes. Otherwise, the two
    processes should be identical.

    Brainsludge, Oct 23, 2007
    1. Advertisements

  2. Brainsludge

    Matimus Guest

    I am running WinXP SP2 with Python 2.5.1 and encountered the following
    You say it exits gracefully, I'm not sure I know what you mean. My
    guess is that you are writing to stdout, and when the buffer fills up
    and tries to dump the program exits with an exception (but returns

    I've run into something similar before, you can reproduce it with
    Code (Text):

    import sys

    f2 = open("bo.err", "w")
    sys.stderr = f2
    for i in range(10000):
        print i
    Run with pythonw then look at "bo.err" to see the exception.

    You can either stop printing to stdout and send the stuff somewhere
    else (maybe use the logging module), or you can assign stdout and
    stderr to nul.

    Code (Text):

    import sys
    import os

    nul = open(sys.devnull,'w')
    sys.stdout = nul
    sys.stderr = nul

    # The rest of your code
    Matimus, Oct 24, 2007
    1. Advertisements

  3. Brainsludge

    Brainsludge Guest

    Hi Matt,

    Your reply is much appreciated.

    So let me see if I understand. When console is running, it dumps
    stdout to console, the buffer is flushed regularly, and everything is
    fine and dandy. But when running without console, the stdout buffer
    fills up because it has no physical handle to dump to, an exception is
    thrown, and the process bails.

    The graceful exit I mentioned before (the cleanup routine) was likely
    due to a try/except I wrapped around the polling loop that handled the
    exception you described.

    I'll give it a try. Thanks!

    Brainsludge, Oct 24, 2007
  4. Brainsludge

    Brainsludge Guest

    The script has been running console-free for about an hour now without
    bailing. Looks like it was the stdout buffer.


    Brainsludge, Oct 24, 2007
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.