我有几种方法是使用多处理模块并行执行的,但是没有按正确的顺序显示输出。例如:
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
当我需要类似的东西时:
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
我试图使用测试脚本创建它,但遇到问题:
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])
我得到下一个输出:
['###########################################', "'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'
在这种情况下如何按IP地址排序以获得上面提到的正确输出?
请帮忙!
答案 0 :(得分:2)
我在这里修改了一些:
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])
这是o / p的样子:
>>> 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