Python Multi-thread thread
#! /usr/bin/env python#-*-coding:utf-8-*-from Threading Import threadimport subprocessfrom Queue Import Queuenum_threads = 3ips = [' 10.108.100.174 ', ' 119.75.218.77 ', ' 127.0.0.1 ']q = Queue () def pingit (i, Queue): While True: IP = queue.ge T () print "Thread%s is pinging%s"% (i, IP) ret = Subprocess.call (' ping-c 3%s '% IP, shell=true, Stdout=open (' /dev/null ', ' W ')) #正常则返回0, Exception returns 1;stdout=open ('/dev/null ', ' w ') masking ping details if ret! = 0: print "%s is down"% IP Queue.task_done () for I in Xrange (num_threads): #xrang比range好 t = Thread (target=pingit, args= (i, q)) T.setdaemon (True) #设置了setDaemon则线程会随着主线程关闭而关闭, in Python, after the main thread ends, it waits for the child thread to end by default, and the mainline friend exits T.start () for IP in IPs: Q.put (IP) print "Main thread is waiting ..." q.join () print "Done ..."