nmlorg / naim

naim is a console client for AOL Instant Messenger (AIM), AOL I Seek You (ICQ), Internet Relay Chat (IRC), and The lily CMC.
http://naim.n.ml.org/
1 stars 0 forks source link

subprocess seems flaky #27

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Here's a test case to reproduce

1. /modload /path/to/pynaim
2. /pyeval import subprocess, time;
3. /pyeval p=subprocess.Popen(["ping","-c","2","em32.net"]); p.poll();
time.sleep(3); p.wait();

On my machine, i get the following:

[13:47:21] *** Traceback (most recent call last):
[13:47:21] ***  File "<string>", line 1, in <module>
[13:47:21] ***  File "/usr/lib/python2.6/subprocess.py", line 1123, in wait
[13:47:21] ***  pid, sts = os.waitpid(self.pid, 0)
[13:47:21] *** OSError: [Errno 10] No child processes

(you can ignore the fact that stuff is being dumped to stdout)

I think something like this should be happening instead (this is run from a
regular python shell)

>>> p=subprocess.Popen(["ping","-c","2","em32.net"]); p.poll();
time.sleep(3); p.wait();
PING em32.net (72.195.132.72) 56(84) bytes of data.
64 bytes from ip72-195-132-72.ri.ri.cox.net (72.195.132.72): icmp_seq=1
ttl=64 time=0.689 ms
64 bytes from ip72-195-132-72.ri.ri.cox.net (72.195.132.72): icmp_seq=2
ttl=64 time=0.688 ms

--- em32.net ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 0.688/0.688/0.689/0.026 ms
0

Note the "0" at the very end -- this is what p.wait() is returning (instead
of throwing an OSError)

the first p.poll() should return None (because the process hasn't exited
yet).  the sleep(3) should be long enough for the process to complete.  and
the p.wait() should wait for the process to complete and then return.

Note that the following also does not work:
/pyeval p=subprocess.Popen(["ping","-c","2","em32.net"]); p.poll();
time.sleep(1.5); p.poll(); time.sleep(1.5); p.poll()

No errors are thrown, but that last p.poll() should be returning something
other than None

Original issue reported on code.google.com by eminence@gmail.com on 21 Jul 2009 at 5:55

GoogleCodeExporter commented 9 years ago
Seems to behave the same for me, but this particular case seems to just be a
complicated way to do os.system('ping -c 2 em32.net'). Do you have a use for
subprocess that can't be simulated with os.system?

Original comment by nml...@gmail.com on 25 Jul 2009 at 11:28

GoogleCodeExporter commented 9 years ago
I'd like to use the subprocess module to watch the run time of a program, and 
kill it
if it runs for too long.  Something like this:

p = subprocess.Popen(cmd)
start = time.time()
while (time.time() - start < 10):
  p.poll()
  if (p.returncode != None):
    doSomething()
    break
  sleep(0.5)
else:
  print("Error: cmd didn't finish in 10 seconds")

As far as I know, this can't be simulated with os.system

Original comment by eminence@gmail.com on 27 Jul 2009 at 12:12