Several scenarios for customizing the thread pool

Source: Internet
Author: User

thread pool:Program Introduction:

Scenario One: A simple version of the thread pool, each time to create a thread pool;

Scenario Two: Support transfer functions, pass parameters, pass callback functions, immediately terminate all threads, the greatest advantage: thread recycling, save time and resources ★★★★★

Scenario Three: Existing modules, directly call, do not support callback function

programme One:
#!/usr/bin/env python#-*-coding:utf-8-*-import queueimport threading  class ThreadPool (object):      def __init__ (self, max_num=20):         Self.queue = Queue.queue (max_num)         for i in Xrange (max_num):             self.queue.put (Threading. Thread)      def get_thread (self):         Return Self.queue.get ()      def add_thread (self):         self.queue.put (Threading. Thread)   "" "Pool = ThreadPool ( def) func (ARG, p):     print arg     import Time    time.sleep (2)     p.add_thread ()   for I in Xrange (+):     thread = Pool.get_thread ()     t = Thread (target=Func, args= (i, Pool))     t.start () "" " 
scenario two:
#!/usr/bin/env python#-*-coding:utf-8-*-import queueimport threadingimport contextlibimport timeStopEvent = Object () Class ThreadPool (object): Def __init__ (self, max_num): self.q = queue. Queue () Self.max_num = max_num Self.terminal = False self.generate_list = [] Self.free_list = [] def run (self, func, args, Callback=none): "" "Thread pool performs a task:p Aram Func: Task function:p Aram args: any Functions Required Parameters:p Aram callback: A callback function executed after the failure or success of the task, the callback function has two parameters 1, the task function execution state, 2, the task function return value (default is None, that is: Do not execute the callback function): return: If the thread pool is already Terminates, returns true otherwise none "" "If Len (self.free_list) = = 0 and len (self.generate_list) < self.max_num:s        Elf.generate_thread () W = (func, args, callback,) Self.q.put (W) def Generate_thread (self): "" " Create a thread "" "T = Threading. Thread (Target=self.call) T.start () def call (self): "" Loop to get the task function and execute the task function "" "Current_ thread = Threading.currentThread self.generate_list.append (current_thread) event = Self.q.get () while event! = Stopevent: Func, arguments, callback = Event Try:result = func (*arguments) status = T Rue except Exception as E:status = False result = e If callback is no                    T None:try:callback (status, result) except Exception as E: Pass if self.terminal: # False event = stopevent Else:with sel F.worker_state (self.free_list,current_thread): event = Self.q.get () else:self.genera             Te_list.remove (Current_thread) @contextlib. ContextManager def worker_state (self,x,v): X.append (v) Try: Yield Finally:x.remove (v) def close (self): num = Len (self.generate_list) W Hile Num:selF.q.put (stopevent) num-= 1 # terminating thread (emptying queue) def terminate (self): self.terminal = True while SE Lf.generate_list:self.q.put (stopevent) self.q.empty () Import timedef work (i): Time.sleep (1) Print ( i) Pool = ThreadPool () for item in range: Pool.run (func=work, args= (item)) # Pool.terminate () #立即终止所有线程
Programme III,
From concurrent.futures import threadpoolexecutorimport timedef F1 (a):    time.sleep (2)    print (a)    return 1pool=threadpoolexecutor (5) for I in range:    a=pool.submit (f1,i)    # X=a.result () #获取返回值, if any, will block

Several scenarios for customizing the 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.