473,324 Members | 2,178 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

what's wrong with my code using subprocess?

I decide to seperate my data collection routine from my data analysis
and storage program to a seperate process, so I try to use the new
subprocess model in Python 2.4.

The main program spawns the subprocess and receives data from the
pipe. When some event occurs (e.g. the user clicks the 'Stop' button
on GUI), the main program will send the subprocess a command to change
its behavior or ask it to exit.

However, my code (attached below) doesn't work. Under Linux, the output is:
<output>
waiting subprocess exit
Traceback (most recent call last):
File "receiver.py", line 19, in ?
main()
File "receiver.py", line 13, in main
print >>p.stdin, 'exit'
IOError: [Errno 32] Broken pipe
</output>

And Under Windows XP, p.wait() never returns:
<output>
waiting subprocess exit
[hanging here]
</output>

What's wrong?

# collector.py
import threading

class Main(object):
def __init__(self):
self.keep_going = True
self.t = threading.Thread(target=self.work)
self.t.start()

cmd = raw_input()
while cmd != 'exit':
cmd = raw_input()

self.keep_going = False
self.t.join()

def work(self):
while self.keep_going:
print '$' * 82

if __name__ == '__main__':
Main()
# receiver.py (the main program)
from subprocess import Popen, PIPE

def main():
p = Popen(['python', 'collector.py'], stdout=PIPE, stdin=PIPE)
count = 0
for line in p.stdout:
data = line.strip()
# process(data)
count += 1
if count >= 1000:
print >>p.stdin, 'exit'
print 'waiting subprocess exit'
p.wait()

if __name__ == '__main__':
main()
--
Qiangning Hong

I'm usually annoyed by IDEs because, for instance, they don't use VIM
as an editor. Since I'm hooked to that, all IDEs I've used so far have
failed to impress me.
-- Sybren Stuvel @ c.l.python

Get Firefox! <http://www.spreadfirefox.com/?q=affiliates&amp;id=67907&amp;t=1>
Jul 24 '05 #1
1 2113
I see the same behavior as you do. On Windows, the wait() isn't
hanging--what's happening is that the subprocess just never receives
anything.

I don't quite understand why, but it works fine when I change the "if"
clause in receiver.py to this:

if count >= 1000:
p.communicate('exit')
p.wait()
break

Note the final break: if this isn't here, the next for iteration raises
an exception, as p.stdout has been closed by p.communicate.

Jul 25 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Dr. Who | last post by:
I am using the subprocess module in 2.4. Here's the fragment: bufcaller.py: import sys, subprocess proc = subprocess.Popen('python bufcallee.py', bufsize=0, shell=True, stdout=subprocess.PIPE)...
4
by: Marc Carter | last post by:
I am trying to rewrite a PERL automation which started a "monitoring" application on many machines, via RSH, and then multiplexed their collective outputs to stdout. In production there are lots...
14
by: Ernesto | last post by:
What's the easiest and quickest way to execute a compiled C "command line interface" program THROUGH Python?
1
by: Jim | last post by:
Hello, I'm trying to use subprocess to drive a Perl script. I'm having some trouble getting it to spot the command line arguments. Basically, if I call subprocess(args).wait() where args has a...
5
by: Grant Edwards | last post by:
I'm trying to use the py-gnuplot module on windows, and have been unable to get it to work reliably under Win2K and WinXP. By default, it uses popen(gnuplotcmd,'w'), but in some situations that...
9
by: Phoe6 | last post by:
Hi all, Consider this scenario, where in I need to use subprocess to execute a command like 'ping 127.0.0.1' which will have a continuous non- terminating output in Linux. # code # This...
0
by: Gabriel Genellina | last post by:
En Thu, 25 Sep 2008 09:49:31 -0300, Almar Klein <almar.klein@gmail.com> escribió: Use subprocess.PIPE Usually the tricky part is to figure out exactly whether there is more input or not. With...
7
by: Samuel A. Falvo II | last post by:
I have a shell script script.sh that launches a Java process in the background using the &-operator, like so: #!/bin/bash java ... arguments here ... & In my Python code, I want to invoke...
1
by: Mark Shewfelt | last post by:
Hello, I am attempting to use Popen() in a Windows service. I have a small Win32 .exe that I normally run through the os.popen2() function. I've written a class to work with the input and output...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.