0

I am trying to start a java process that is meant to take a long time, using python's subprocess module.

What I am actually doing is using the multiprocessing module to start a new Process, and using that process, use subprocess module to run java -jar.

This works fine, but when I start the new process, the java process replaces the python process running python Process. I would like java to run as a child process in a way that when the process that started a new multiprocessing.Process died, the process running java would die too.

Is this possible?

Thanks.

Edit: here's some code to clarify my question:

def run_task():
    pargs = ["java -jar app.jar"]
    p = Popen(pargs)
    p.communicate()[0]
    return p

while(True):
    a = a_blocking_call()

    process = Process(target=run_task)
    process.start()

    if not a:
        break

I want the process running run_task to be killed along with the process running java when the process executing the while loop reaches the break line. Is this possible?

simao
  • 14,491
  • 9
  • 55
  • 66
  • Maybe I misunderstood the question, but why do you need the `multiprocessing` module? You can start child processes with the `subprocess` functions alone. – Philipp Jan 24 '11 at 13:15
  • I need to start the new process and then continue running the while loop to start new processes. – simao Jan 24 '11 at 14:09

1 Answers1

0

I think you should show some code, it's not clear how you are using subprocess and multiprocessing together.

From the documentation it looks like subprocess should spawn and not replace your new Process-started process. Are you sure that isn't happening ? A test case showing it doesn't would be good.

You may get some hints out of Detach a subprocess started using python multiprocessing module

Community
  • 1
  • 1
Thomas Vander Stichele
  • 36,043
  • 14
  • 56
  • 60