Python Multi-thread thread

Source: Internet
Author: User
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 ..."
  • 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.