开发者

change process state with python

开发者 https://www.devze.com 2023-04-12 08:26 出处:网络
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)`

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,th开发者_StackOverflow社区e 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.


you can get the output generated by this process, reading the stdout descriptor:

out = p.stdout.read()

0

精彩评论

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