Results 1 to 3 of 3
Hi all
I have a Python program looks like this one :
Code:
import os
import time
process = os.popen("top").readlines()
time.sleep(1)
os.popen("killall top")
print process
the program hangs in :
...
- 12-11-2010 #1Just Joined!
- Join Date
- Jul 2010
- Posts
- 26
Stop reading process output in Python without hang ?
Hi all
I have a Python program looks like this one :
the program hangs in :Code:import os import time process = os.popen("top").readlines() time.sleep(1) os.popen("killall top") print process
because "Top" tool and that happens in the tools that keep update outputtingCode:process = os.popen("top").readlines()
my best trials :
it worked better than the first one (it's kelled ), but it returns :Code:import os import time import subprocess process = subprocess.Popen('top') time.sleep(2) os.popen("killall top") print process
the second trial :Code:<subprocess.Popen object at 0x97a50cc>
the same as the first one. It hanged due to "readlines()"Code:import os import time import subprocess process = subprocess.Popen('top').readlines() time.sleep(2) os.popen("killall top") print process
...
I want its return like this :
Code: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 ?
Last edited by Barakat; 12-11-2010 at 02:19 AM.
- 12-11-2010 #2
first off, don't use os.popen since its deprecated call
second, subprocess.popen returns a process object, so if you want output in there, you will have to change what you do
capturing output from top is hard, because of the nature of the application
17.1. subprocess ? Subprocess management — Python v2.7.1 documentation
- 12-12-2010 #3Just Joined!
- Join Date
- Jul 2010
- Posts
- 26
thanks coopstah13 for the replay, the problem has been solved :
and this is a much better code by ( J.F. Sebastian ) :Code:import os import subprocess import time subprocess.Popen("top >> tmp_file",shell = True) time.sleep(1) os.popen("killall top") process = os.popen("cat tmp_file").read() os.popen("rm tmp_file") print process
Code:import collections import subprocess import time import threading def read_output(process, append): for line in iter(process.stdout.readline, ""): append(line) def main(): # start process, redirect stdout process = subprocess.Popen(["top"], stdout=subprocess.PIPE, close_fds=True) try: # save last `number_of_lines` lines of the process output number_of_lines = 200 q = collections.deque(maxlen=number_of_lines) # atomic .append() t = threading.Thread(target=read_output, args=(process, q.append)) t.daemon = True t.start() # time.sleep(2) finally: process.terminate() #NOTE: it doesn't ensure the process termination # print saved lines print ''.join(q) if __name__=="__main__": main()


Reply With Quote