Python's method of implementing thread pool

Source: Internet
Author: User
Tags mutex thread

This article illustrates how Python implements the thread pool. Share to everyone for your reference. Specifically as follows:

Principle: Create a task queue, but many threads from this task queue to take out the task and then execute, of course, the task queue to lock, detailed see the code

File name: thrd_pool.py system Environment: Ubuntu Linux & python2.6

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 import threading import time import signal import OS class Task_info (object) : Def __init__ (self): Self.func = None Self.parm0 = None Self.parm1 = None Self.parm2 = None class Task_list (object): Def __init__ (self): self.tl = [] Self.mutex = Threading. Lock () Self.sem = Threading. Semaphore (0) def append (self, ti): Self.mutex.acquire () self.tl.append (TI) self.mutex.release () self.sem.release () def Fetch (self): Self.sem.acquire () self.mutex.acquire () Ti = Self.tl.pop (0) self.mutex.release () Return TI class THRD ( Threading. Thread): Def __init__ (self, TL): Threading. Thread.__init__ (self) self.tl = TL def run (self): while True:tsk = Self.tl.fetch () tsk.func (Tsk.parm0, Tsk.parm1, Tsk.par m2) class Thrd_pool (objECT): def __init__ (self, Thd_count, tl): Self.thds = [] for I in range (Thd_count): Self.thds.append (THRD (TL)) def run (self ): For THD in Self.thds:thd.start () def func (Parm0=none, Parm1=none, parm2=none): print ' count:%s, thrd_name:%s ' (str (par M0), Threading.currentthread (). GetName ()) def cleanup (Signo, stkframe): Print (' oops! Got signal%s ', Signo) os._exit (0) If __name__ = ' __main__ ': signal.signal (signal. SIGINT, Cleanup) signal.signal (signal. Sigquit, Cleanup) signal.signal (signal. Sigterm, cleanup) tl = task_list () TP = Thrd_pool (6, TL) tp.run () Count = 0 while true:ti = task_info () Ti.parm0 = Count Ti.func = Func tl.append (ti) count = 1 Time.sleep (2) Pass

How to execute: Python thrd_pool.py

Execution results:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14-15 16 count:0, Thrd_name:thread-1 count:1, Thrd_name:thread-2 count:2, thrd_name:thread-3 count:3, thrd_name:Thread-4 count : 4, Thrd_name:thread-5 count:5, thrd_name:thread-1 count:6, thrd_name:thread-6 count:7, Thrd_name:Thread-2 Count:8, Thrd_name:thread-3 Count:9, thrd_name:thread-4 count:10, thrd_name:thread-5 count:11, Thrd_name:Thread-1 count:12, Thrd_name:thread-6 count:13, Thrd_name:thread-2 count:14, thrd_name:thread-3 (' oops! Got signal%s ', 15)

I hope this article will help you with your Python programming.

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.