停止读取 Python 中的进程输出而不挂起?
我有一个Linux的Python程序几乎看起来像这样:
import os
import time
process = os.popen("top").readlines()
time.sleep(1)
os.popen("killall top")
print process
程序挂在这一行:
process = os.popen("top").readlines()
这发生在不断更新输出的工具中,如“Top”
我最好的试验:
import os
import time
import subprocess
process = subprocess.Popen('top')
time.sleep(2)
os.popen("killall top")
print process
它比第一个程序工作得更好(它被杀死了),但它返回:
<subprocess.Popen object at 0x97a50cc>
第二次试验:
import os
import time
import subprocess
process = subprocess.Popen('top').readlines()
time.sleep(2)
os.popen("killall top")
print process
与第一次相同。它由于“readlines()”而挂起
它的返回应该是这样的:
top - 05:31:15 up 12:12, 5 users, load average: 0.25, 0.14, 0.11
Tasks: 174 total, 2 running, 172 sleeping, 0 stopped, 0 zombie
Cpu(s): 9.3%us, 3.8%sy, 0.1%ni, 85.9%id, 0.9%wa, 0.0%hi, 0.0%si, 0.0%st
Mem: 1992828k total, 1849456k used, 143372k free, 233048k buffers
Swap: 4602876k total, 0k used, 4602876k free, 1122780k cached
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
31735 Barakat 20 0 246m 52m 20m S 19.4 2.7 13:54.91 totem
1907 root 20 0 91264 45m 15m S 1.9 2.3 38:54.14 Xorg
2138 Barakat 20 0 17356 5368 4284 S 1.9 0.3 3:00.15 at-spi-registry
2164 Barakat 9 -11 164m 7372 6252 S 1.9 0.4 2:54.58 pulseaudio
2394 Barakat 20 0 27212 9792 8256 S 1.9 0.5 6:01.48 multiload-apple
6498 Barakat 20 0 56364 30m 18m S 1.9 1.6 0:03.38 pyshell
1 root 20 0 2880 1416 1208 S 0.0 0.1 0:02.02 init
2 root 20 0 0 0 0 S 0.0 0.0 0:00.02 kthreadd
3 root RT 0 0 0 0 S 0.0 0.0 0:00.12 migration/0
4 root 20 0 0 0 0 S 0.0 0.0 0:02.07 ksoftirqd/0
5 root RT 0 0 0 0 S 0.0 0.0 0:00.00 watchdog/0
9 root 20 0 0 0 0 S 0.0 0.0 0:01.43 events/0
11 root 20 0 0 0 0 S 0.0 0.0 0:00.00 cpuset
12 root 20 0 0 0 0 S 0.0 0.0 0:00.02 khelper
13 root 20 0 0 0 0 S 0.0 0.0 0:00.00 netns
14 root 20 0 0 0 0 S 0.0 0.0 0:00.00 async/mgr
15 root 20 0 0 0 0 S 0.0 0.0 0:00.00 pm
并保存在变量“process”中。伙计们,我现在真的陷入困境了吗?
I have a Python program for Linux almost looks like this one :
import os
import time
process = os.popen("top").readlines()
time.sleep(1)
os.popen("killall top")
print process
the program hangs in this line :
process = os.popen("top").readlines()
and that happens in the tools that keep update outputting like "Top"
my best trials :
import os
import time
import subprocess
process = subprocess.Popen('top')
time.sleep(2)
os.popen("killall top")
print process
it worked better than the first one (it's kelled ), but it returns :
<subprocess.Popen object at 0x97a50cc>
the second trial :
import os
import time
import subprocess
process = subprocess.Popen('top').readlines()
time.sleep(2)
os.popen("killall top")
print process
the same as the first one. It hanged due to "readlines()"
Its returning should be like this :
top - 05:31:15 up 12:12, 5 users, load average: 0.25, 0.14, 0.11
Tasks: 174 total, 2 running, 172 sleeping, 0 stopped, 0 zombie
Cpu(s): 9.3%us, 3.8%sy, 0.1%ni, 85.9%id, 0.9%wa, 0.0%hi, 0.0%si, 0.0%st
Mem: 1992828k total, 1849456k used, 143372k free, 233048k buffers
Swap: 4602876k total, 0k used, 4602876k free, 1122780k cached
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
31735 Barakat 20 0 246m 52m 20m S 19.4 2.7 13:54.91 totem
1907 root 20 0 91264 45m 15m S 1.9 2.3 38:54.14 Xorg
2138 Barakat 20 0 17356 5368 4284 S 1.9 0.3 3:00.15 at-spi-registry
2164 Barakat 9 -11 164m 7372 6252 S 1.9 0.4 2:54.58 pulseaudio
2394 Barakat 20 0 27212 9792 8256 S 1.9 0.5 6:01.48 multiload-apple
6498 Barakat 20 0 56364 30m 18m S 1.9 1.6 0:03.38 pyshell
1 root 20 0 2880 1416 1208 S 0.0 0.1 0:02.02 init
2 root 20 0 0 0 0 S 0.0 0.0 0:00.02 kthreadd
3 root RT 0 0 0 0 S 0.0 0.0 0:00.12 migration/0
4 root 20 0 0 0 0 S 0.0 0.0 0:02.07 ksoftirqd/0
5 root RT 0 0 0 0 S 0.0 0.0 0:00.00 watchdog/0
9 root 20 0 0 0 0 S 0.0 0.0 0:01.43 events/0
11 root 20 0 0 0 0 S 0.0 0.0 0:00.00 cpuset
12 root 20 0 0 0 0 S 0.0 0.0 0:00.02 khelper
13 root 20 0 0 0 0 S 0.0 0.0 0:00.00 netns
14 root 20 0 0 0 0 S 0.0 0.0 0:00.00 async/mgr
15 root 20 0 0 0 0 S 0.0 0.0 0:00.00 pm
and save in the variable "process". Any I idea guys, I'm really stuck now ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
仅打印部分输出的类似尾部的解决方案
您可以在另一个线程中读取进程输出,并将所需数量的最后几行保存在队列中:
此变体需要
q.append()
来是原子操作。否则输出可能会损坏。signal.alarm()
解决方案您可以使用
signal.alarm()
来调用指定超时后的 process.terminate() ,而不是在另一个线程中读取。尽管它可能无法与subprocess
模块很好地交互。基于 @Alex Martelli 的回答:此方法仅适用于 *nix 系统。如果 process.stdout.readline() 不返回,它可能会阻塞。
threading.Timer
解决方案这种方法也应该在 Windows 上工作。这里我使用了 process.stdout 作为可迭代对象;它可能会引入额外的输出缓冲,如果不需要,您可以切换到 iter(process.stdout.readline, "") 方法。如果进程未在
process.terminate()
处终止,则脚本将挂起。无线程,无信号解决方案
此变体既不使用线程,也不使用信号,但会在终端中产生乱码输出。如果 process.stdout.readline() 阻塞,它将阻塞。
Tail-like Solutions that print only the portion of the output
You could read the process output in another thread and save the required number of the last lines in a queue:
This variant requires
q.append()
to be atomic operation. Otherwise the output might be corrupted.signal.alarm()
solutionYou could use
signal.alarm()
to call theprocess.terminate()
after specified timeout instead of reading in another thread. Though it might not interact very well with thesubprocess
module. Based on @Alex Martelli's answer:This approach works only on *nix systems. It might block if
process.stdout.readline()
doesn't return.threading.Timer
solutionThis approach should also work on Windows. Here I've used
process.stdout
as an iterable; it might introduce an additional output buffering, you could switch to theiter(process.stdout.readline, "")
approach if it is not desirable. if the process doesn't terminate onprocess.terminate()
then the scripts hangs.No threads, no signals solution
This variant use neither threads, no signals but it produces garbled output in the terminal. It will block if
process.stdout.readline()
blocks.我建议不要使用“top”,而是使用“ps”,它会为您提供相同的信息,但只会提供一次,而不是永远每秒一次。
您还需要在 ps 中使用一些标志,我倾向于使用“ps aux”
Instead of using "top" I suggest using "ps" which will give you the same information, but only once instead of once a second for all eternity.
You'll need to also use some flags with ps, I tend to use "ps aux"
我要做的,而不是这种方法,是检查您试图从中获取信息的程序,并确定该信息的最终来源。它可能是 API 调用或设备节点。然后,编写一些从同一源获取它的Python。这消除了“抓取”“熟”数据的问题和开销。
What I would do, rather than this approach, is to examine the program you are trying to get information from and determine the ultimate source of that information. It may be an API call or device node. Then, write some python that gets it from the same source. That eliminates the problems and overhead of "scraping" "cooked" data.
(JF Sebastian,你的代码工作得很好,我认为它比我的解决方案更好=))
我已经用另一种方式解决了它。
我没有直接在终端上输出,而是将其放入文件“tmp_file”中:
然后我使用工具“cut”将其输出“这是顶部输出”作为进程的值
,并且它做了我想要它做的事情。这是最终的代码:
非常感谢大家的帮助
( J.F. Sebastian your codes work great , I think it's better than my solution =) )
I've solved it using another way.
Instead of making the output directly on the terminal I make it into a file "tmp_file" :
then I used the tool "cut" to make its output "which is top output" as process's value
and it did what I want it to do .This is the final code:
Thank you so much guys for help
事实上,如果您填充输出缓冲区,您最终会得到一些答案。因此,一种解决方案是用大量垃圾输出填充缓冲区(大约 6000 个字符,bufsize=1)。
比方说,你有一个在 sys.stdout: 上编写的 python 脚本,而不是 top:
在启动器方面,而不是简单的 process.readline():
很确定它有点脏,因为 2000 依赖于子进程实现,但它工作正常而且非常简单。设置除 bufsize=1 之外的任何值都会使情况变得更糟。
In facts, if you fill the output buffer, you'll end with some answer. So one solution is to fill the buffer with a large garbage output (~6000 character with bufsize=1).
Let's say, instead of top, you have a python script that write on sys.stdout:
On the launcher side, instead of simple process.readline():
Quite sure it is a bit dirty as 2000 is dependant on subprocess implementation, but it works fine and is very simple. setting anything but bufsize=1 make the matter worse.