简体   繁体   English

用python改变进程状态

[英]change process state with python

I want to search for a process and show it,emacs for example,I use 我想搜索一个进程并显示它,例如emacs,我使用

`p = subprocess.Popen('ps -A | grep emacs',shell=True,stdout=subprocess.PIPE)` 

to get the process, then how can I wake it up and show it? 为了得到这个过程,我该如何将其唤醒并显示出来?

in other words,the question shoud be : how python change the state of process? 换句话说,问题应该是:python如何改变进程的状态?

In short, python has a pty module and look for the solution there. 简而言之,python有一个pty模块并在那里寻找解决方案。

This question is not that simple as it may look like. 这个问题并不像看起来那么简单。

It is simple to change the running state of a process by delivering corresponding signals but it is not simple to manipulate foreground/background properties. 通过提供相应的信号来改变过程的运行状态很简单,但操纵前景/背景属性并不简单。

When we talk about manipulating the foreground/background processes, we really talk about 'job control.' 当我们谈论操纵前景/后台流程时,我们真的谈论“工作控制”。 In UNIX environment, job control is achieved by coordination of several parts, including kernel, controlling terminal, current shell and almost every process invoked in that shell session. 在UNIX环境中,作业控制是通过协调几个部分来实现的,包括内核,控制终端,当前shell以及在该shell会话中调用的几乎每个进程。 Telling a process to get back to foreground, you have to tell others to shut up and go to background simultaneously. 告诉过程回到前台,你必须告诉别人闭嘴并同时转到后台。 See? 看到?

Let's come back to your question. 让我们回到你的问题。 There could be 2 answers to this, one is no way and the other is it could be done but how. 可能有2个答案,一个是没有办法,另一个是可以完成但是如何。

Why 2 answers? 为什么2个答案?

Generally you cannot have job control unless you program for it. 一般来说,除非你为它编程,否则你无法控制工作。 You also cannot use a simple pipe to achieve the coordination model which leads to job control mechanism; 您也不能使用简单的管道来实现导致作业控制机制的协调模型; the reason is essential since you cannot deliver signals through a pipe. 原因很重要,因为你无法通过管道传递信号。 That's why the answer is no way, at least no way in a simple pipe implementation. 这就是为什么答案是没有办法的,至少在简单的管道实现中没有办法。

However, if you have enough patience to program terminal I/O, it still can be done with a lot of labor work. 但是,如果您有足够的耐心来编程终端I / O,那么仍然可以通过大量的人力工作来完成。 Concept is simple: you cheat your slave program, which is emacs in this example, that it has been attached to a real terminal having a true keyboard and a solid monitor standby, and you prepare your master program, which is the python script, to handle and relay necessary events from its controlling terminal to the slave's pseudo-terminal. 概念很简单:你欺骗你的奴隶程序,在这个例子中是emacs,它已被附加到具有真正键盘和实体监视器待机的真实终端,并且你准备你的主程序,即python脚本,到处理并将必要的事件从其控制终端中继到从属的伪终端。

This schema is actually adopted by many terminal emulators. 许多终端仿真器实际上采用了这种模式。 You just need to write another terminal emulator in your case... Wait! 你需要在你的情况下编写另一个终端模拟器......等等! Does it have to be done with so much effort, always? 总是需要付出这么多努力吗?

Luckily no. 幸运的是没有。

Your shell manages all the stuff for you in an interactive scenario. 您的shell在交互式场景中为您管理所有内容。 You just tell shell to ' fg/bg ' the task, quite easy in real life. 你只需告诉shell'fg / bg '这个任务,在现实生活中很容易。 The designated command combination can be found in shell's manpage. 指定的命令组合可以在shell的联机帮助页中找到。 It could look like ' jobs -l | 它可能看起来像' jobs -l | grep emacs ' along with ' fg %1 '. grep emacs '和' fg%1 '。 Nonetheless those combined commands cannot be invoked by a program. 尽管如此,程序无法调用这些组合命令。 It's a different story since a program will start a new shell to interpret its commands and such a new shell cannot control the old running emacs because it doesn't have the privilege. 这是一个不同的故事,因为一个程序将启动一个新的shell来解释它的命令,这样一个新的shell无法控制旧的运行emacs,因为它没有这个特权。 Type it in with your keyboard and read it out on your monitor; 用键盘输入,然后在显示器上读出; that's an interactive scenario. 这是一个互动场景。

In an automation scenario, think twice before you employ a job control design because most automation scenarios do not require a job control schema. 在自动化方案中,在使用作业控制设计之前要三思,因为大多数自动化方案不需要作业控制模式。 You need an editor here and a player there, that's all right, but just don't make them to stay "background" and pop to "foreground." 你需要一个编辑器和一个玩家,没关系,但是不要让它们保持“背景”并弹出“前景”。 They'd better exit when they complete their task. 当他们完成任务时,他们最好退出。

But if you are unlucky to have to program job control in automation procedures, try to program pseudo-terminal master and slave I/O as well. 但是,如果您不幸在自动化程序中编写作业控制,请尝试编写伪终端主从I / O. They look like a sophisticated IPC mechanism and their details are OS-dependent. 它们看起来像一个复杂的IPC机制,它们的细节依赖于操作系统。 However this is the standard answer to your question; 然而,这是您问题的标准答案; though annoying, I know. 虽然烦人,但我知道。

你可以获得这个过程生成的输出,读取stdout描述符:

out = p.stdout.read()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM