python多线程Ping
[*]#!/usr/bin/python
[*]# -*- coding: utf-8 -*-
[*]
[*]import os
[*]import threading
[*]import Queue
[*]
[*]queue = Queue.Queue()
[*]_thread = 150
[*]
[*]f = open('ip.list','r')
[*]for ip in f.readlines():
[*] k = ip.strip()
[*] queue.put(k)
[*]f.close()
[*]
[*]def check(i,q):
[*] while True:
[*] ip=q.get()
[*] data = os.system("ping -c 1 %s > /dev/null 2>&1" % ip)
[*] if data==0:
[*] print "%s: running ok\n" % ip
[*] else:
[*] print "%s: running no\n" % ip
[*] q.task_done() #表示queue.join()已完成队列中提取元组数据
[*]for i in range(_thread):
[*] run=threading.Thread(target=check,args=(i,queue)) #创建一个threading.Thread()的实例,给它一个函数和函数的参数
[*] run.setDaemon(True)#这个True是为worker.start设置的,如果没有设置的话会挂起的,因为check是使用循环实现的
[*] run.start()
[*]queue.join()#线程队列执行关闭
[*]print "ping task running done."
页:
[1]