A detailed introduction to the thread of multiple threads in python

Source: Internet
Author: User
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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.