python 使用队列实现线程池

#-*- coding: utf-8 -*-
'''
Created on 2019��1��1��

@author: Administrator
'''
import Queue
import threading
from time import sleep
taskQueue = Queue.Queue()
def worker(fun):
    while True:
        temp = taskQueue.get()
        fun(temp)
        taskQueue.task_done()
def threadPool(threadPoolNum):
    threadPool = []
    for i in xrange(threadPoolNum):
        thread = threading.Thread(target=worker,args={doFun,})
#         设置为守护线程,当主线程退出后,守护线程也会退出。如果不设置成守护线程,线程将因为执行的是死循环而无法退出
#         主线程将阻塞与子线程
        thread.daemon = True
        threadPool.append(thread)
    return threadPool

def doFun(param):
    print '%s execute %s' % (threading.current_thread(),param)
if __name__ == '__main__':
    pool1 = threadPool(3)
    for i in xrange(pool1.__len__()):
#         可以先启动线程,因为如果任务队列taskQueue为空时,调用get()函数是会阻塞
        pool1[i].start()
    for i in xrange(10):
        taskQueue.put(i)
#          队列调用的join函数,任何加入队列项都是是计数加一,对于任何子线程只要调用task_done()函数都会
#          使计数减一,但未完成任务的计数减为0时,join函数就不会阻塞.初始计数为0
    taskQueue.join()
    pass
 execute 0
 execute 1
 execute 2
 execute 3
 execute 4
 execute 5
 execute 6
 execute 7
 execute 8
 execute 9

二、改进版 线程池执行不同的任务

#-*- coding: utf-8 -*-
'''
Created on 2019��1��1��

@author: Administrator
'''
import Queue
import threading
from time import sleep
taskQueue = Queue.Queue()
def worker():
    while True:
        dofun,params = taskQueue.get()
        dofun(params)
        taskQueue.task_done()
def threadPool(threadPoolNum):
    threadPool = []
    for i in xrange(threadPoolNum):
        thread = threading.Thread(target=worker)
#         设置为守护线程,当主线程退出后,守护线程也会退出。如果不设置成守护线程,线程将因为执行的是死循环而无法退出
#         主线程将阻塞与子线程
        thread.daemon = True
        threadPool.append(thread)
    return threadPool

def doFun1(params):
    print '%s execute doFun1' % (threading.current_thread().getName())
    print params[0]
    print params[1]
def doFun2(params):
    print '%s execute doFun2' % (threading.current_thread().getName())
if __name__ == '__main__':
    pool1 = threadPool(3)
    for i in xrange(pool1.__len__()):
#         可以先启动线程,因为如果任务队列taskQueue为空时,调用get()函数是会阻塞
        pool1[i].start()
    params = ('az1','true')
    for i in xrange(5):    
        taskQueue.put((doFun1,params))
    params = ()
    for i in xrange(5):    
        taskQueue.put((doFun2,params))
    
#          队列调用的join函数,任何加入队列项都是是计数加一,对于任何子线程只要调用task_done()函数都会
#          使计数减一,但未完成任务的计数减为0时,join函数就不会阻塞.初始计数为0
    taskQueue.join()
    pass

Thread-1 execute doFun1
Thread-2 execute doFun1
Thread-3 execute doFun1
Thread-1 execute doFun1
Thread-2 execute doFun1
Thread-2 execute doFun2
Thread-2 execute doFun2
Thread-1 execute doFun2
Thread-3 execute doFun2
Thread-2 execute doFun2

你可能感兴趣的:(python)