The path of small white growth: a preliminary knowledge of Python (vi)--python thread pool

Source: Internet
Author: User

#!/usr/bin/env python
#-*-Coding:utf-8-*-
Import threading
Import queue
Import time
"""
Follow the teacher's course to do a thread pool, the main idea is to carry out the task to put into the queue
Several threads are then created to continuously retrieve the task from the queue and execute
Compared to the low B version of the thread pool has been greatly improved, let's call a version of lower a ...
"""

Stop_flag = Object ()

Class ThreadPool (object):
def __init__ (Self,max_num):
Self.max_num = Max_num

#创建一个队列用于保存任务
Self.queue = queue. Queue ()
#创建一个列表保存已经创建的线程
Self.generate_list = []
#创建一个列表保存当前空闲的线程
Self.free_list = []
#是否结束任务的标志
Self.terminate_flag = False

def run (self, func, args, Callback=none):
W = (func, args, callback,)
Self.queue.put (W) #把相关的参数放入队列当中
If Len (self.generate_list) < Self.max_num and Len (self.free_list) = = 0:
Self.generate ()

def generate (self):
t = Threading. Thread (Target=self.call)
T.start ()

def call (self):
# # # #获取当前的线程对象, and added to the list of created threads
Current_thread = Threading.current_thread ()
Self.generate_list.append (Current_thread)

Work = Self.queue.get ()
#从队列中获取相关的任务信息
While work! = Stop_flag:
Func, args, callback = Work
Try
ret = func (*args)
Except Exception as E:
Print (e)
If callback:
Try
Callback (ret)
Except Exception as ex:
Print (ex)


# # #上面为一个线程执行一次任务的完整流程, once the thread is created, the task is continuously fetched from the queue
##### #在执行完一次任务和获取下一次任务的空当内, Thread is idle
If Self.terminate_flag:
Break
Else
Self.free_list.append (Current_thread)
Work = Self.queue.get ()
Self.free_list.append (Current_thread)
# #在开始时或者在循环过程中接收到Stop_Flag时 will execute the following statement
Self.generate_list.remove (Current_thread)


def close (self):
#当只是完成上面的代码的情况下, the main process does not end immediately, ^ ^ because this time there is no Stop_flag incoming queue
For I in range (len (self.generate_list)):
Self.queue.put (Stop_flag)

def terminate (self):
Self.terminate_flag = True



def do (i):
# Time.sleep (0.5)
Print (i)
Def c ():
Pass

Pool = ThreadPool (10)

For I in range (50):
Pool.run (Func=do, args= (i,))

Pool.terminate ()
# Pool.close ()

Small white growth path: first knowledge Python (vi)--python 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.