thread hangs when using subprocess only in specific circumstances

  • Thread starter Toon Verstraelen
  • Start date
T

Toon Verstraelen

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
 
D

Dennis Lee Bieber

--- 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.
--
 
D

Donn Cave

Dennis Lee Bieber said:
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, (e-mail address removed)
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,962
Messages
2,570,134
Members
46,690
Latest member
MacGyver

Latest Threads

Top