Python multithreaded +queue__python

Source: Internet
Author: User

Python's queue is designed to be thread-safe , so be assured that the big guy is using it.
A simple implementation of Python multithreading is as follows:

#!/usr/bin/env python
#-*-coding:utf-8-*-
Import Threading
Import time

def fun (argv):
    print ' In ', argv
    time.sleep (2)

threads = []    #用于保存线程 to
I in range (5):  #开5个线程
    t = Threading. Thread (target = fun, args = str (i))
    threads.append (t)

if __name__ = = ' __main__ ':
    #开始所有的线程 for
    i in th reads:
        i.start ()
    #保证线程执行完 for    
    i-Threads:
        i.join ()
    print ' all over '

The results of the program implementation are as follows

Using threading can easily open multiple threads, the role of join is used for thread synchronization, to see their own needs.
OK, now go to the point, multithreading +queue How to get, first give the code

#!/usr/bin/env python #-*-coding:utf-8-*-import Queue import threading Import Time thread_id = 1 Thread_num = 3 Clas S Mythread (threading. Thread): Def __init__ (self, Q): Global thread_id Threading. Thread.__init__ (self) self.q = Q self.
                thread_id = thread_id thread_id = thread_id + 1 def run (self): while True:try: Task = self.q.get (block = True, timeout = 1) #不设置阻塞的话会一直去尝试获取资源 except Queue.Empty:prin T ' Thread ', self. thread_id, ' End ' break #取到数据, start processing (depending on the requirements of the processing code) print "Starting", self. thread_id Print Task Self.q.task_done () print "Ending", self. thread_id q = queue.queue #向资源池里面放10个数用作测试 for I in Range (a): Q.put (i) #开Thread_num个线程 for I in range (0, THR

 Ead_num): worker = Mythread (q) Worker.start () q.join () #等待所有的队列资源都用完 print "Exiting Main Thread"

The

results are as follows

is not feeling very messy, haha does not matter, this is the charm of multithreading. You never know which thread to start with. The
queue here as a resource pool, thread to take data from the resource pool to process, but why need to use queue, because the resource pool is very large, and the number of threads are limited, so when a thread finishes its task, it can continue to fetch resources processing. This allows the thread that performs one task to starters not to stop immediately, but to fetch another resource to process it until it stops when there is no data. Simple understanding: Q.task_done is to indicate that the current resource is finished, and Q.join () will wait until all resources have been processed to proceed downward, which is a synchronization.

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.