Spawn new process -> get pid

B

breal

I have a soap server written in Python that acts as an intermediary
between a web service and an InDesign server. The indesign server is
non-threaded, so when all instances are used up I want to create a new
instance, get the pid, use the process, then kill it.

What is the best way to do this? I don't want to replace the current
process with the new one which is looks like os.exec* does.

Thanks.
 
G

Gabriel Genellina

I have a soap server written in Python that acts as an intermediary
between a web service and an InDesign server. The indesign server is
non-threaded, so when all instances are used up I want to create a new
instance, get the pid, use the process, then kill it.

What is the best way to do this? I don't want to replace the current
process with the new one which is looks like os.exec* does.

Use the subprocess module:
http://docs.python.org/lib/module-subprocess.html
 
G

Gabriel Genellina

Thanks for the reply.  Using subprocess.Popen I am able to get and
kill the process.  However, the process I am running creates another
process (an instance of InDesign Server) so when I kill the first
process it doesn't kill the second.  Is there any way to capture both
PIDs and kill them?

I assume you're on *nix. Can you modify the first process? Make it a
project group leader, then kill the whole group. I don't remember the
details, try `man setpgrp` (or setpg?)
 
B

breal

I assume you're on *nix. Can you modify the first process? Make it a
project group leader, then kill the whole group. I don't remember the
details, try `man setpgrp` (or setpg?)

Ok, so at first I was calling a shell script that called another
program to start. I have eliminated the middle-man shell script and
am now calling the program directly. But here is my problem...

I need the program to start fully before I go on any further so that
it is available later in my script. After it is used I need to kill
the pid of the program I started. So, I was trying to use
my_process = subprocess.Popen(["/path/to/my/program', "-port", "%s"
%port_number, "-configure", "conf.%s" %port_number], stdout=f1)
my_process.wait()
child_pid = my_process.pid

Is this the right way to do it? It seems to be returning a different
pid for the process... so when I try to kill it I get a "no such
process" error.

Basically I have five instances of InDesign server running on various
ports.. 18400 - 18404. When they are all in use, I want to start a
new instance... have it fully running... then use it and kill it.

Any help is appreciated.
 
G

Gabriel Genellina

I need the program to start fully before I go on any further so that
it is available later in my script. After it is used I need to kill
the pid of the program I started. So, I was trying to use
my_process = subprocess.Popen(["/path/to/my/program', "-port", "%s"
%port_number, "-configure", "conf.%s" %port_number], stdout=f1)
my_process.wait()
child_pid = my_process.pid

Is this the right way to do it? It seems to be returning a different
pid for the process... so when I try to kill it I get a "no such
process" error.

wait() waits until the process finishes. You can't kill it later - it's
already dead :)
Basically I have five instances of InDesign server running on various
ports. 18400 - 18404. When they are all in use, I want to start a
new instance... have it fully running... then use it and kill it.

What means "fully working"? I guess you could try to connect to the port
used, and when the connection succeeds, it's ready.
 

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,994
Messages
2,570,222
Members
46,810
Latest member
Kassie0918

Latest Threads

Top