因为/dev/null 所以紧适用于linux,而且是2的python版本
下面这个程序,是一个多线程的例子,下面num_threads =3 限定了3个进程。如果ips的值多余3个,比如说4个,也是可以顺利执行的,就是第0个线程执行完之后,会产生第二个0的线程进行执行。很厉害的样子
为什么用先进先出队列呢,因为最大线程数往往需要固定一个值。
#!/usr/bin/python
#coding=utf-8
from threading import Thread
import subprocess
from Queue import Queue #插入队列的构造方法
num_threads = 3
ips = ['192.168.1.111','192.168.1.1','192.168.1.161'] #计划需要ping的地址
q = Queue()
def pingit(i,queue):
while True:
ip = queue.get() #从队列中取出一个值
print "thread %s is pinging %s" %(i,ip)
ret = subprocess.call('ping -c 3 %s' %ip,shell=True,stdout=open('/dev/null','w')) #这里导致了,只能在linux下面的python用,正常则返回0,异常则返回一,通过是否正常返回来判断通断,stdout=open('/dev/nu