- 论坛徽章:
- 0
|
本帖最后由 xxhacker 于 2012-12-19 15:05 编辑
各位,小弟刚学python不久,参照网上写了一个多进程PING的脚本,可是输出的时候就很乱,有些进程先执行完,有些后执行完,问一下有没有方法按照按IP排序一下再输出了?- #! /usr/bin/python
- import sys
- import subprocess
- from multiprocessing import Process,Queue
- num_process = 10
- queue = Queue()
- def pinger (q):
- while True:
- if q.empty():
- sys.exit()
- ip = q.get()
- ret = subprocess.call("ping -c 1 %s" %ip,shell=True,stdout=open("/dev/null","w"),stderr=subprocess.STDOUT)
- if ret == 0:
- print "%s: is [\033[32;1mONLine\033[0m]" %ip
- else:
- print "%s: is [\033[31;1mOFFLine\033[0m]" %ip
- for num in range(1,255):
- ip = "192.168.1." + str(num)
- queue.put(ip)
- for i in range(num_process):
- worker = Process(target=pinger,args=(queue))
- worker.start()
- worker.join()
复制代码 |
|