2016-06-30 11 views
0

Ich habe mehrere Methoden, die parallel ausgeführt werden, indem Multiprocessing-Modul, aber zeigen Sie nicht Ausgabe in der richtigen Reihenfolge. Zum Beispiel:Sortieren stdout durch IP-Adresse in Python

Host:10.76.0.114 Running upon kernel reinstallation for GRUB 
Host:10.76.0.114 Running: /sbin/new-kernel-pkg --package kernel --mkinitrd --dracut --depmod --install --multiboot=/boot/xen.gz 3.18.34-20.el6.x86_64 

Host:10.76.0.121 Running %pre for the backup-tools 
Host:10.76.0.112 Setting up Update Process 
Host:10.76.0.121 Running: yum -y -q update httpd 

Wenn ich brauche so etwas wie:

Host:10.76.0.112 Setting up Update Process 
Host:10.76.0.114 Running upon kernel reinstallation for GRUB 
Host:10.76.0.114 Running: /sbin/new-kernel-pkg --package kernel --mkinitrd 
Host:10.76.0.121 Running %pre for the backup-tools 
Host:10.76.0.121 Running: yum -y -q update httpd 

ich versuche es erstellt Testskript verwenden, aber mit Problemen mit ihm:

import StringIO 
import string, sys 

stdout = sys.stdout 

sys.stdout = file = StringIO.StringIO() 

print """ 
10.76.0.114 Initiate Recovery images download 
10.76.0.114 Running: wget -b -N -i /tools/recovery/recovery-templates-url.list -o /tools/recovery/recovery-templates-download.log -P /tools/recovery >/dev/null 2>&1 
10.76.0.114 2016-06-30 00:33 Finished Xen Hypervisor '4.2' and StorageAPI '4.2' install 
10.76.0.114 
10.76.0.113 Preparing...    ########################################### [100%] 
10.76.0.113 package 3-2.noarch is already installed 


""" 

sys.stdout = stdout 

l=file.getvalue() 
#print l 
l=str(l) 
l=l.split() 
#print l 
print sorted(l, key=lambda k:k[0]) 

ich die nächsten Ausgabe :

['###########################################', "'4.2'", "'4.2'", '-b', '-N', '-i', '-o', '-P', '/tools/recovery/recovery-templates-url.list', '/tools/recovery/recovery-templates-download.log', '/tools/recovery', '00:33', '10.76.0.114', '10.76.0.114', '10.76.0.114', '10.76.0.114', '10.76.0.113', '10.76.0.113', '10.76.0.113', '2>&1', '2016-06-30', '>/dev/null', 'Finished', 'Hypervisor', 'Initiate', 'Preparing...', 'Recovery', 'Running:', 'Retrieving', 'StorageAPI', 'Xen', '[100%]', 'and', 'already', 'download', 'images', 'install', 'is', 'installed', , 'package', 'wget' 

H Kann ich in diesem Fall nach IP-Adresse sortieren, um die korrekte Ausgabe zu erhalten, die ich oben erwähnt habe?

Bitte helfen!

Antwort

2

Hier habe ich einige geändert:

import StringIO 
import string, sys 

stdout = sys.stdout 

sys.stdout = file = StringIO.StringIO() 

print """ 
10.76.0.114 Initiate Recovery images download 
10.76.0.114 Running: wget -b -N -i /tools/recovery/recovery-templates-url.list -o /tools/recovery/recovery-templates-download.log -P /tools/recovery >/dev/null 2>&1 
10.76.0.114 2016-06-30 00:33 Finished Xen Hypervisor '4.2' and StorageAPI '4.2' install 
10.76.0.114 
10.76.0.113 Preparing...    ########################################### [100%] 
10.76.0.113 package 3-2.noarch is already installed 


""" 

sys.stdout = stdout 

text = file.getvalue() 
lines = [line for line in text.splitlines() if line] 
print sorted(lines, key=lambda line: line.split(' ')[0]) 

Hier ist, wie die o/p aussehen wird:

>>> for sorted_line in sorted(lines, key=lambda line: line.split(' ')[0]): 
...  print sorted_line 
10.76.0.113 Preparing...    ########################################### [100%] 
10.76.0.113 package 3-2.noarch is already installed 
10.76.0.114 Initiate Recovery images download 
10.76.0.114 Running: wget -b -N -i /tools/recovery/recovery-templates-url.list -o /tools/recovery/recovery-templates-download.log -P /tools/recovery >/dev/null 2>&1 
10.76.0.114 2016-06-30 00:33 Finished Xen Hypervisor '4.2' and StorageAPI '4.2' install 
10.76.0.114