编程执行和终止在蟒蛇长时间运行的批处理 [英] programmatically executing and terminating a long-running batch process in python

查看:176
本文介绍了编程执行和终止在蟒蛇长时间运行的批处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找一种方式来启动和终止蟒长期运行的批处理作业。现在,我使用使用os.system()推出了长时间运行的批处理作业中的每个子进程中。正如你可能已经猜到了,使用os.system()派生的子进程(孙进程吗?)内的新的进程,所以我不能杀了从盛大父进程批处理作业。为了提供我刚才所描述的一些可视化:

I have been searching for a way to start and terminate a long-running "batch jobs" in python. Right now I'm using "os.system()" to launch a long-running batch job inside each child process. As you might have guessed, "os.system()" spawns a new process inside that child process (grandchild process?), so I cannot kill the batch job from the grand-parent process. To provide some visualization of what I have just described:

Main (grandparent) process, with PID = AAAA
          |
          |------> child process with PID = BBBB
                         |
                         |------> os.system("some long-running batch file)
                                  [grandchild process, with PID = CCCC]

所以,我的问题是我无法从祖父母杀死孙子的过程...

So, my problem is I cannot kill the grandchild process from the grandparent...

我的问题是,是否有一个启动子进程里长时间运行的批处理作业方式,并能杀死只需终止子进程批处理作业?
什么是使用os.system(),我可以使用,这样我可以杀了从主过程中的批处理作业的替代方案?

My question is, is there a way to start a long-running batch job inside a child process, and being able to kill that batch job by just terminating the child process? What are the alternatives to os.system() that I can use so that I can kill the batch-job from the main process ?

谢谢!

推荐答案

模块是在Python产卵和控制过程的正确方法。

subprocess module is the proper way to spawn and control processes in Python.

从文档:

该子模块允许您
  产卵新工艺,连接到他们的
  输入/输出/错误管道,并获得
  他们返回codeS。 这个模块
  打算更换其他几个,
  旧的模块和功能
,如:

使用os.system 结果os.spawn结果os.popen结果popen2结果命令

os.system
os.spawn
os.popen
popen2
commands

所以...如果你是Python的2.4 + 是替换使用os.system

so... if you are on Python 2.4+, subprocess is the replacement for os.system

有关停止进程,请查看终止()通讯()的方法 POPEN 的对象。

for stopping processes, check out the terminate() and communicate() methods of Popen objects.

这篇关于编程执行和终止在蟒蛇长时间运行的批处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆