This article shares a detailed introduction to the thread of python multithreading. This article shares a detailed introduction to the thread of python multithreading.
Thread of python multithreading
#! /Usr/bin/env python #-*-coding: UTF-8-*-from threading import Threadimport subprocessfrom Queue import Queuenum_threads = 3ips = ['10. 108.100.174 ', '2017. 75.218.77 ', '2017. 0.0.1 '] 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') # if it is normal, 0 is returned, 1 is returned if an exception occurs. stdout = open ('/dev/n Ull ', 'w') blocks ping details if ret! = 0: print "% s is down" % ip queue. task_done () for I in xrange (num_threads): # xrang is better than range t = Thread (target = pingit, args = (I, q) t. setDaemon (True) # When setDaemon is set, the thread will be closed with the main thread closed. in python, after the main thread ends, the main thread will exit t by default after the sub-thread ends. start () for ip in ips: q. put (ip) print "main thread is waiting... "q. join () print "Done..."
The above is a detailed introduction to the thread of python multithreading. For more information, see other related articles in the first PHP community!