Tough Spawn Problem

G

googlemike

I'm trying to use Python and PyGTK + Glade, along with Bash. I want to
make a GUI for the Linux vpnclient command-line tool. (Look for
linux-vpnclient.tar.gz on the Internet if you are curious.) Anyway,
this vpnclient tool connects to VPN and then stays locked like that
while the VPN connection is running. What I was trying to do was either
spawn it and then use another command-line tool to check on its status,
or shell the process and redirect all its output text to the GTK
textbox in the GUI. Here's the problems I found with that:

* Spawn - This seems to spawn the process okay, but when the vpnclient
connection is finally loaded, it holds the process and the GUI cannot
return to being responsive -- it locks the GUI up, essentially. I tried
os.NO_WAIT and os.DETACH but os.NO_WAIT was no solution and os.DETACH
only works in the documentation, not in my version of Python.

* Shell - This seems to do the same thing as spawn but instead of
sending output to a console, it sends the output into a console-less
space. Then, because the VPN connection is locked-in, it also holds the
GUI up and hangs it.

I tried using the " &" operator but that seemed to have no effect in
fixing this problem.

What's the trick?
 
J

Jeff Epler

By using os.spawn* and the os.P_NOWAIT, the spawn function will return
immediately, with the return value being the PID of the new process.
Later, you can use os.kill() to force the program to terminate,
os.waitpid() to retrieve the exit status if it has terminated, or you
could use the signal module to wait for SIGCHLD to be delivered at the
time the child terminates.

With os.spawn*, the child's open files (including stdin and stdout) are
the same as the parent's; using the popen2 module, you can send the
program input and capture its output too.

Here's the program I ran on Linux (Fedora Core 2 / Python 2.3) to show
that os.P_NOWAIT works fine:
import os
pid = os.spawnv(os.P_NOWAIT, "/bin/sh",
["sh", "-c", "sleep 1; echo spawned program"])
print "child is pid", pid
print "waitpid result is", os.waitpid(pid, 0)
and the output is
$ python /tmp/googlemike.py
child is pid 13874
spawned program
waitpid result is (13874, 0)
the fact that "spawned program" is printed after "child is pid NNNNN"
shows that the python program continues executing while the child is
working (or, in this case, sleeping).

Jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (GNU/Linux)

iD8DBQFCKw3XJd01MZaTXX0RAqwDAKCN/ya+3R0Jy+O3sJSF1TBnhjV40ACgrZKg
zBbog4AwadFOuxx/6SgqpCA=
=EObs
-----END PGP SIGNATURE-----
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top