Paul Malherbe wrote:
nephish wrote:
Hey there,
i have a program that calls another program to execute on a button
click. Its another pygtk app. And i am using the
os.system('python /home/my/myprogram.py') to bring it up. It comes up
ok, but when i close its window, the program that called it up is locked
up. Um, what could i do about this?
thanks.
Hi
Instead of using os.system(), try:
os.spawnv(os.P_WAIT, ...) to execute the child process and wait for it
to end or
os.spawnv(os.P_NOWAIT, ...) to executr the child process and continue
without waiying for it to end.
Regards
Paul
The variable sys.executable contains the path of the current python
interpreter. It's good to use it (rather than just 'python') when
starting other python scripts. On some systems this might be
'/usr/local/bin/python2.4' for example.
For a more pleasant and powerful API than os.spawn*, you might want to
look at the 'subprocess' module that is new in Python 2.4. To start a
process, use:
p = subprocess.Popen([sys.executable, '/home/my/myprogram.py'])
To wait for that process "p" to complete, and get it's return code:
retcode = p.wait()
If you don't want to block, but still want to know when the process has
completed, this simplest solution is probably be to use a timeout
handler to poll the process:
def display_process_complete(retcode):
"""Modify the interface to show that a process has completed"""
pass
def wait_for_process(p):
retcode = p.poll()
if retcode is None:
return True # repeat the timeout
else:
gtk.threads_enter()
try:
display_process_complete(retcode)
return False # stop the timeout
finally:
gtk.threads_leave()
p = subprocess.Popen([sys.executable, '/home/my/myprogram.py'])
gtk.timeout_add(500, p)
A more accurate watch could be kept on the subprocesses by handling the
SIGCHLD signal, but this would be more complex and not portable to
Windows. I'll leave it as an exercise for the reader :)
--
Tim Evans
Applied Research Associates NZ
http://www.aranz.co.nz/
_______________________________________________
pygtk mailing list [email protected]
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/