thread hangs when using subprocess only in specific circumstances

Discussion in 'Python' started by Toon Verstraelen, Jan 9, 2006.

  1. Hi,

    I recently had a thread problem and I could reduce it to a very short
    example that shows the problem. I hope it has its origin in my
    misunderstanding of how python threads work. Here it is:

    --- my_thread.py ---

    import threading
    from subprocess import Popen, PIPE, STDOUT

    class MyThread(threading.Thread):
    def run(self):
    print "before Popen"
    sp = Popen(["ls", "-al"], stdout=PIPE, stderr=STDOUT)
    print "before first line"
    for line in sp.stdout:
    print line[:-1]
    print "after last line"


    t = MyThread()
    t.start()

    --- main.py ---

    import my_thread


    ----------------

    If I start my_thread.py directly there is no problem, but when i run
    'python main.py' the thread hangs on the creation of the Popen object. I'm
    using python 2.4.2 on gentoo linux. Can someone reproduce the problem?

    Thanks for reading this.

    Toon
     
    Toon Verstraelen, Jan 9, 2006
    #1
    1. Advertising

  2. On Mon, 09 Jan 2006 22:59:45 +0100, Toon Verstraelen
    <> declaimed the following in comp.lang.python:

    > --- main.py ---
    >
    > import my_thread
    >

    Are you sure it is hanging? I'd expect this to do the import
    (starting the thread in the import context) and then to immediately exit
    since there is no code in the main thread to wait for anything.
    --
    > ============================================================== <
    > | Wulfraed Dennis Lee Bieber KD6MOG <
    > | Bestiaria Support Staff <
    > ============================================================== <
    > Home Page: <http://www.dm.net/~wulfraed/> <
    > Overflow Page: <http://wlfraed.home.netcom.com/> <
     
    Dennis Lee Bieber, Jan 10, 2006
    #2
    1. Advertising

  3. Toon Verstraelen

    Donn Cave Guest

    In article <>,
    Dennis Lee Bieber <> wrote:

    > On Mon, 09 Jan 2006 22:59:45 +0100, Toon Verstraelen
    > <> declaimed the following in comp.lang.python:
    >
    > > --- main.py ---
    > >
    > > import my_thread
    > >

    > Are you sure it is hanging? I'd expect this to do the import
    > (starting the thread in the import context) and then to immediately exit
    > since there is no code in the main thread to wait for anything.


    But this would also be true when he runs the module directly,
    right?

    I don't know about that, but when I run it via import, instead
    of hanging on NetBSD 2.0 it crashes, and you get the file and
    line number of the pthread function where it died. Shortly
    after doing a yield that's apparently expected to always work.

    If not via import, no problem. If not subprocess, no problem
    (I can use os.popen, or os.pipe/fork/execve, etc.) I imagine
    this is related to the problem on Linux.

    Donn Cave,
     
    Donn Cave, Jan 10, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    664
  2. Richard Thompson
    Replies:
    11
    Views:
    588
    Richard Thompson
    Feb 6, 2005
  3. Ron
    Replies:
    9
    Views:
    621
    CBFalconer
    Mar 10, 2007
  4. Ivo
    Replies:
    7
    Views:
    959
    Roedy Green
    Jun 4, 2009
  5. mehdi_mousavi
    Replies:
    0
    Views:
    154
    mehdi_mousavi
    Sep 28, 2005
Loading...

Share This Page