[Baypiggies] multithreading question

Shannon -jj Behrens jjinux at gmail.com
Thu Sep 18 08:55:11 CEST 2008

I think the easiest thing to do is:

from __future__ import with_statement
import threading

# This is to protect stdout and stderr.

output_lock = threading.Lock()

def printf(s, file=sys.stdout):
    """Print s to file and then flush it.

    This function uses output_lock so that all writes to stdout and
    stderr are synchronized.

    Note, the caller is responsible for adding the newline.

    with output_lock:

I looked into using the logging module, but it turns out that the one
place I write to stdout is via the csv module:

        with output_lock:

Hence, I use printf instead of print, and I explicitly manage a lock
in the one place where I can't use printf.


More information about the Baypiggies mailing list