Source code parsing of a python thread pool, python thread source code

Source: Internet
Author: User

Source code parsing of a python thread pool, python thread source code

Python highly encapsulates many things to facilitate people's programming, such as the process pool in the process, which greatly facilitates the efficiency of People's programming, but there is no thread pool by default, I have sorted out a thread pool some time ago and made some simple parsing and comments. I am at a limited level. If I have any mistakes, I hope you can learn and make progress together, source code

Import threading, time, queuestop = object () class Thread (object): def _ init _ (self, max_num): # constructor self. q = queue. queue () # create a Queue to store the task self. max_num = max_num # maximum number of threads in the thread pool self. terminal = False self. generate_list = [] # list of actually created threads self. free_list = [] # idle thread list def generate_thread (self): # create a process and execute the function t = threading. thread (target = self. call) t. start () def call (self): # obtain the task and execute the task function current_thread = threading. cur Using thread # obtain the current thread self. generate_list.append (current_thread) # Add the list even = self. q. get () # get the task while even from the queue! = Stop: # cyclically execute the func, args, callback = even try: ret = func (args) # execute the function status = True when T Exception as e: # if the error status is False, the execution result fu is assigned with ret status = False ret = e if callback is not None: # if no callback function is available, try: callback (status, ret) failed t Exception as e: pass if self. terminal: # p determine whether to terminate even = stop else: self. free_list.append (current_thread) # after execution, add the thread to idle even = self. q. get () # obtain self again. free_list.remove (c Urrent_thread) # change the status to non-idle else: self. generate_list.remove (current_thread) # If no task exists, the def run (self, func, args, callback = None) element in the actually created thread list will be deleted ): # method w = (func, args, callback,) self. q. put (w) # put the task into the queue if len (self. free_list) = 0 and len (self. generate_list) <self. max_num: # determine whether to create thread self. generate_thread () def close (self): # disable the thread function num = len (self. generate_list) while num: self. q. put (stop) num-= 1 def Terminal (self): # a function that can be forcibly terminated when the task is not executed. self. terminal = True # according to self. terminal judge max_num = len (self. generate_list) while max_num: # Put in the stop of the number of the List length to end the blocked process self. q. put (stop) max_num-= 1 # This method clears the thread but does not clear the queue task def terminall (self ): # A function that can be forcibly terminated when the task is not executed, self. terminal = True # according to self. terminal determines while self. generate_list: # If the list is not empty, it will be placed into the sotp clearing thread list self. q. put (stop) self. q. empty () # It is perfect to clear the queue after the thread is cleared. Def work (a): # The following is an example of print (a) pool = Thread (10) for I in range (50): pool. run (func = work, args = I) pool. close ()

 

Related Article

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.