简体   繁体   中英

change process state with python

I want to search for a process and show it,emacs for example,I use

`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?

In short, python has a pty module and look for the solution there.

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. 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.

Why 2 answers?

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. 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.

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. You just tell shell to ' fg/bg ' the task, quite easy in real life. The designated command combination can be found in shell's manpage. It could look like ' jobs -l | grep emacs ' along with ' 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. 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. They look like a sophisticated IPC mechanism and their details are OS-dependent. However this is the standard answer to your question; though annoying, I know.

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

out = p.stdout.read()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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