FAQ
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

Search Discussions

  • Donn Cave at Jan 10, 2006 at 12:54 am
    In article <qgt5s154jgqa9dc5fb41vp717ghpmd8fdr at 4ax.com>,
    Dennis Lee Bieber wrote:
    On Mon, 09 Jan 2006 22:59:45 +0100, Toon Verstraelen
    <Toon.Verstraelen at UGent.be> 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 at u.washington.edu

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedJan 9, '06 at 9:59p
activeJan 10, '06 at 12:54a
posts2
users2
websitepython.org

2 users in discussion

Donn Cave: 1 post Toon Verstraelen: 1 post

People

Translate

site design / logo © 2022 Grokbase