开发者

Start long running process using subprocess module

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 runn开发者_JAVA技巧ing 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?


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜