Python Simple thread pool

Source: Internet
Author: User
Class ThreadPool (object):    def __init__ (self, max_num=20):        # Create a queue with a maximum of 10 data        Self.queue = queue. Queue (max_num) # populate thread classes in Queues # "Thread class, Thread class, Thread class, Thread class, Thread class, thread class,        thread class" for        I in range (max_num):            self.queue.put ( Threading. Thread)    def get_thread (self): # go to the        queue to go to the data,        # queue characteristics, if any, to joins that one out        #            if not, block,        return Self.queue.get ()    def add_thread (self):        # Add a thread class Self.queue.put (threading) to the queue        . Thread) # pool = ThreadPool (ten) Pool = ThreadPool (+) def func (ARG, p):    # function Content    print (ARG)    import    time Time.sleep (2)    # Online constructor add a thread back to the thread pool    p.add_thread () for I in range:    # go to the thread pool and if there is one, take it in the pool, If not, wait until someone returns the thread to the thread pool thread    = Pool.get_thread ()    # thread = Threading. Thread    t = Thread (Target=func, args= (i, pool))    T.start ()

  

Python Simple thread pool

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.