Unusual Python interpreter problem with os.fork()

Jean-Paul Calderone exarkun at divmod.com
Fri Mar 6 08:20:29 EST 2009


On Fri, 6 Mar 2009 05:00:03 -0800 (PST), DLitgo <ssj4_dave at hotmail.com> wrote:
>Hello everyone,
>
>I have a curious problem which I'm wondering if anyone here can shed
>some light on. I'm basically just following along with a guide which
>is going through some of the os module, and I'm running some examples
>in the python interpreter on mac os x (accessed through terminal/
>bash).
>
>Basically all I did was use os.fork() which caused this strange
>problem:
>
>Macintosh:~ $ python
>Python 2.5.1 (r251:54863, Jan 13 2009, 10:26:13)
>[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
>Type "help", "copyright", "credits" or "license" for more information.
>>>>
>>>> import os
>>>> pid = os.fork()

As soon as this returns, you have two CPython processes reading from stdin
and writing to stdout.  They fight over your input and their output gets
interleaved in non-deterministic ways.  Basically, you probably don't ever
want to do this.

Jean-Paul



More information about the Python-list mailing list