Python path "Eighth" Python implements thread pool

Source: Internet
Author: User

Thread pool Concept


Many server applications, such as Web servers, database servers, file servers, and mail servers, are geared toward processing a large number of short tasks from some remote sources. A simplistic model of the
build server application is to create a new service object whenever a request arrives, and then service the request in the new service object.
However, when there are a large number of requests for concurrent access, it is expensive for the server to constantly create and destroy objects.
so one way to improve server efficiency is to minimize the number of objects created and destroyed, especially the resource-intensive object creation and destruction, which introduces the concept of "pooling",
"Pool" Concept allows people to customize a certain amount of resources and then reuse those resources, rather than creating and destroying them frequently.

thread pool is a technique for pre-creating threads .
These threads are all asleep, that is, they are started and do not consume the CPU, but only occupy a small amount of memory space.
When the request arrives, the buffer pool allocates an idle thread to the request, which passes the request to the thread to run and process.
When pre-created threads are running, that is, the thread pool is free to create a certain number of new threads to handle more requests.
When the system is idle, you can also remove a portion of a thread that has been in a deactivated state.

thread pool considerations
Although the thread pool is a powerful mechanism for building multithreaded applications, using it is not without risk. When using the thread pool, pay attention to the relationship between thread pool size and performance, and pay attention to issues such as concurrency risk, deadlock, insufficient resources, and thread leaks.
1, thread pool size. Multithreaded applications not as many threads as possible, need to determine the size of the thread pool based on the hardware and software environment in which the system is running and the characteristics of the application itself.

In general, if the code is structured properly, the number of threads is appropriate for the number of CPUs.
If the thread is running, it can increase the size of the pool and, if necessary, use an adaptive algorithm to dynamically adjust the size of the thread pool to improve the efficient utilization of the CPU and the overall performance of the system.
2, concurrency error. Multi-threaded application to pay special attention to concurrency errors, to logically guarantee the correctness of the program, pay attention to avoid the occurrence of deadlock phenomenon.
3, thread leakage. This is a serious problem in the thread pool application, where a thread leak occurs when the task finishes and the thread fails to return to the pool.

Thread Pool Essentials

Thread Pool essentials:

thread pool essentials:1, by judging the number of tasks to wait and the maximum value in the thread pool, take the minimum value to determine how many threads to open work such as: The number of tasks is 3, the maximum process pool  , then we just need to open 3 threads on the line. The number of tasks is 500, the process pool is 20, then we can only open 20 threads. Take the minimum value 2, the implementation of the threadpool is running, there is a view of the function, look at the current thread inside the active thread is how much wait for how much? The total number of threads, the number of waits, how many functions are running: easy to see the current thread pool state can be obtained after this can be used when the thread has been idle view state with: Context management to do, very nice point 3, close thread

Simple thread Pool Implementation

#!/usr/bin/env python#-*-coding:utf-8-*-__author__='luo_t'ImportQueueImportThreadingImport Time" "The idea of this simple example is through: 1, using the queue feature to create multiple thread objects in the queue 2, when I execute the code, go to the queue to get the thread! If the thread pool is available, take it directly. If the line constructor is not available, then wait. 3, the thread execution is complete, return to the thread pool" "classThreadPool (object):#Creating a thread pool class    def __init__(SELF,MAX_THREAD=20):#construction method, setting the maximum number of threads toSelf.queue = Queue.queue (max_thread)#Create a queue         forIinchXrange (Max_thread):#loops Add thread objects to the queueSelf.queue.put (Threading. Thread)#Put the thread's class name in and execute the queue    defGet_thread (self):#defining methods to fetch threads from a queue        returnSelf.queue.get ()defAdd_thread (self):#defining methods to add threads to a queueSelf.queue.put (Threading. Thread) Pool= ThreadPool (10)deffunc (arg,p):PrintArg time.sleep (2) P.add_thread ()#The current thread is done, I'm adding a thread to the queue!  forIinchXrange (300): Thread= Pool.get_thread ()#thread pool 10 threads, each cycle take one! The default Queue.get () waits if there is no data in the queue. t = Thread (target=func,args=(I,pool)) T.start ()" "Self.queue.put (Threading. Thread) added is that the class is not an object, in memory if the same class occupies only a portion of memory space and if the object is stored here, each time the new will have to open up a memory space in memory and if it is an object: The following statement can not be called so! For i in xrange: thread = pool.get_thread () t = Thread (target=func,args= (I,pool)) T.start () by looking at the source code you can know that the T    In the constructor of hread: Self.__args = args Self.__target = target is a private field then the call should be written for the I in xrange: ret = Pool.get_thread () Ret._thread__target = Func Ret._thread__args = (i,pool) ret.start ()" "
simple_pool.py

The knowledge points that complex thread pooling needs to know

#!/usr/bin/env python#-*-coding:utf-8-*-__author__='luo_t'ImportQueueobj= Object ()#object is also a class, I created an object objQ=Queue.queue () forIinchRange (10):    PrintID (obj)#look at the turnip .q.put (obj)" "There are 10 radishes (radish =obj) in this queue, but these 10 carrots are just a projection. We put it in the queue for the For loop, does obj change? Is there a new space to open? Apparently not ." "
knowledge_point_1.py
#!/usr/bin/env python#-*-coding:utf-8-*-__author__='luo_t'ImportContextlibImportThreadingImport TimeImportrandomdoing= []defNumber (L2): whileTrue:PrintLen (L2) Time.sleep (1) T= Threading. Thread (target=number,args= (doing,))#open a thread, print the list every second, the number of threads currently in workT.start ()#add an adorner for the admin context@contextlib. ContextManagerdefShow (Li,iterm): Li.append (iterm)yield    " "yield freezes this operation, goes out, and with Will catch, then executes with the code block, and when the code block with the execution is finished, it will return to the code block that does not execute under yield! And then it's done. If the with code block is very time-consuming, then does the length of the doing always be 1, indicating that he has not finished the execution? We can get to the number that is being executed when he executes a subsequent code block of yield after execution is complete.    After he was removed, it was 0! " "Li.remove (iterm)defTask (ARG): With show (doing,1):#switching through the with management context        PrintLen (doing) time.sleep (10)#Wait 10 seconds Here you can use the random module to manipulate ~ forIinchRange (20):#Open 20 Thread executiontemp = Threading. Thread (target=task,args=(i,)) Temp.start ()" "role: We want to record a working list such as a working thread I add to doing this list, if the work is done, remove it from the doing list. With this mechanism, you can get the number of threads that are now executing" "
knowledge_point_2.pyThread Pool Implementation
#!/usr/bin/env python#-*-coding:utf-8-*-__author__='luo_t' fromQueueImportQueueImportContextlibImportThreadingworkerstop=object ()classthreadpool:workers=0 Threadfactory=Threading. Thread CurrentThread=Staticmethod (Threading.currentthread)def __init__(Self, maxthreads=20, name=None): self.q= Queue (0)#here to create a queue, if it is 0 means no limit, now this queue is a taskSelf.max = MaxThreads#define the maximum number of threadsSelf.name =name Self.waiters= []#these two are for counting.Self.working = []#These two are for technology.    defStart (self):#Self.max Maximum number of threads        #Q.qisze (), Number of tasksNeedsize =self.q.qsize () whileSelf.workers < min (Self.max, needsize):#min (10,20) take minimum value            #wokers default is 0 "workers = 0"            " "For example: while Self.workers < min (Self.max, needsize): This loop, such as the maximum thread is 20, we have a task number of 10, take the minimum A value of 10 per loop open 1 threads, and workers self-increment 1, then the Loop 10 times, opened 10 threads workers = 10, then the workers is not less than 10 will not open the thread, I am the maximum number of threads, you 10 of the threads            Go and consume these 10 tasks and do not block here, create a thread to execute! Every thread is going to execute the _worker method." "Self.startaworker ()defStartaworker (self): Self.workers+ = 1Newthread= Self.threadfactory (Target=self._worker, name='Shuaige')#Create a thread and execute the _worker methodNewthread.start ()defCallinthread (Self, func, *args, * *kw): Self.callinthreadwithcallback (None, func,*args, * *kw)defCallinthreadwithcallback (self, Onresult, func, *args, * *kw): o=(func, args, KW, Onresult) self.q.put (o) @contextlib. ContextManagerdef_workerstate (self, StateList, workerthread): Statelist.append (workerthread)Try:            yield        finally: Statelist.remove (workerthread)def_worker (self): CT=self.currentthread () o= Self.q.get ()#go to the queue to take the task, if there is a task o will have value, each task is a tuple, there are methods, there are parameters         whileO is  notWorkerstop:with self._workerstate (self.working, CT):#Context Switchesfunction, args, kwargs, Onresult =odeloTry: Result= function (*args, * *Kwargs) Success=Trueexcept: Success=FalseifOnresult isNone:Pass                    Else:                        Pass                delfunction, args, KwargsifOnresult is  notNone:Try: Onresult (success, result)except:                        #Context.call (CTX, Log.err)                        Pass                delOnresult, result with Self._workerstate (self.waiters, CT):#when the thread has finished his spare time, it is going to fetch the task executiono =Self.q.get ()defStop (self):#Defining the Shutdown threading method         whileSelf.workers:#Cyclic workers valueSelf.q.put (Workerstop)#add a signal to the queue ~Self.workers-= 1#workers value-1 until all threads are closeddefShow (ARG):ImportTime Time.sleep (1)    PrintArgpool= ThreadPool (10)#Create 500 tasks with 500 tasks added to the queue#each task is a tuple (method name, dynamic parameter, dynamic parameter, default = None) forIinchRange (100): Pool.callinthread (show, I) Pool.start ()#after the queue is added, turn on the thread and let the thread go to the queue and get it .pool.stop ()#when all the above tasks are done, the threads are waiting for the data to be in the queue! " "we are going to close all the threads, execute the Stop method, first workers this value is the current number of threads, we send a signal to the thread "Workerstop" in the course of the work: while O was not workerstop: if the thread gets to this The value is not executed, and then the thread is stopped while looping, waiting for the Python garbage collection mechanism to be recycled. Then in self.workers-= 1, then all the threads will stop after receiving this signal!!! over~" "

For more information, please refer to: http://www.cnblogs.com/wupeiqi/articles/4839959.html

Python path "Eighth" Python implements 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.