possible to preserve subprocess.Popen objects for later?

R

Ratko

Hi all,

I have a python gui app that launches multiple applications using
subprocess.Popen class and prints their output in the gui (using
PIPEs, threads and wxPython). Everything works great but the problem
is that some applications should run in the background (ie they don't
close when the gui closes) so next time when you start the gui it will
not have a handle on those processes that are still running and
therefore won't be able to print their output.

So, I was wondering if there is any way to "preserve" the Popen object
of those background processes and reload them when the gui is
restarted? Ideally I would like to have the whole Popen object
preserved but I could also get by with just the Popen.stdout file
object. I can save the Popen.stdout.fileno() integer because that's
what I use in os.read() to read the output anyway but that doesn't
work.

I have doubts that this could even conceptually work but I thought I'd
try asking anyway. I don't have a full understanding of how processes
and pipes work on the system level...

Thanks,
Ratko
 
D

Diez B. Roggisch

Ratko said:
Hi all,

I have a python gui app that launches multiple applications using
subprocess.Popen class and prints their output in the gui (using
PIPEs, threads and wxPython). Everything works great but the problem
is that some applications should run in the background (ie they don't
close when the gui closes) so next time when you start the gui it will
not have a handle on those processes that are still running and
therefore won't be able to print their output.

So, I was wondering if there is any way to "preserve" the Popen object
of those background processes and reload them when the gui is
restarted? Ideally I would like to have the whole Popen object
preserved but I could also get by with just the Popen.stdout file
object. I can save the Popen.stdout.fileno() integer because that's
what I use in os.read() to read the output anyway but that doesn't
work.

I have doubts that this could even conceptually work but I thought I'd
try asking anyway. I don't have a full understanding of how processes
and pipes work on the system level...

This doesn't work. What could work is that you create a python spawning
process that lives as long as all it's popened childrend - and then let
the gui connect to that, by whatever IPC means you like. E.g. pyro.

Diez
 
Z

zacherates

Hi all,

I have a python gui app that launches multiple applications using
subprocess.Popen class and prints their output in the gui (using
PIPEs, threads and wxPython). Everything works great but the problem
is that some applications should run in the background (ie they don't
close when the gui closes) so next time when you start the gui it will
not have a handle on those processes that are still running and
therefore won't be able to print their output.

So, I was wondering if there is any way to "preserve" the Popen object
of those background processes and reload them when the gui is
restarted? Ideally I would like to have the whole Popen object
preserved but I could also get by with just the Popen.stdout file
object. I can save the Popen.stdout.fileno() integer because that's
what I use in os.read() to read the output anyway but that doesn't
work.

I have doubts that this could even conceptually work but I thought I'd
try asking anyway. I don't have a full understanding of how processes
and pipes work on the system level...

Thanks,
Ratko

Sounds like you might want to consider running the backround task as
either a service or daemon and communicate via a TCP/IP connection or
a named pipe rather than using STDIN/OUT. Actually you could use
either idea without actually setting up a daemon.

Also, you're generally supposed to wait() for your children to die
(even if you stop the gui) rather than let them become zombies.

Cheers,
Aaron
 
R

Ratko

Sounds like you might want to consider running the backround task as
either a service or daemon and communicate via a TCP/IP connection or
a named pipe rather than using STDIN/OUT. Actually you could use
either idea without actually setting up a daemon.

Unfortunately I don't have control over the processes that I am
running (they are components of a much larger system that I am trying
to bring under one roof).

However, it seems that a named pipe might work. Is it possible to set
up a named pipe for a process that I start and tell that process to
direct all its output to it?

Thanks,
Ratko
 

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,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top