python多线程

#coding=utf-8

 

import os

import sys

import threading

import Queue

import time

default_encoding = 'utf-8'

if sys.getdefaultencoding() != default_encoding:

    reload(sys)

    sys.setdefaultencoding(default_encoding)

 

class MutiThread(threading.Thread):

    def __init__(self, workQueue, resultQueue, timeout = 3, **kwargs):

        threading.Thread.__init__(self, kwargs = kwargs)

        self.setDaemon(True)

        self.timeout = timeout

        self.workQueue = workQueue

        self.resultQueue = resultQueue

 

    def run(self):

        while True:

            try:

                job, args = self.workQueue.get(timeout = self.timeout)

                result = job(args)

                self.resultQueue.put("%s|%s" % (result, self.getName()))

            except Queue.Empty:

                break

            except:

                print sys.exc_info()

                raise

 

class ThreadPool:

    def __init__(self, threadPoolSize = 10):

        self.worketQueue = Queue.Queue()

        self.resultQueue = Queue.Queue()

        self.threads = []

        self.__createThreadPool(threadPoolSize)

 

    def __createThreadPool(self, threadPoolSize):

        for i in range(threadPoolSize):

            threadObj = MutiThread(self.worketQueue, self.resultQueue)

            self.threads.append(threadObj)

 

    def waitForComplete(self):

        while len(self.threads):

            thread = self.threads.pop()

            if thread and thread.isAlive():

                thread.join()

 

    def addJob(self, job, args):

        self.worketQueue.put((job, args))

 

    def startThreads(self):

        for th in self.threads:

            th.start()

 

def testJob(arg1):

    print "start test job:%s" % (arg1)

    time.sleep(1)

    print "end test job:%s" % (arg1)

    return (arg1)

 

def test():

    print 'start testing'

    tp = ThreadPool(5)

    for i in range(40):

        time.sleep(0.2)

        tp.addJob(testJob, i)

    tp.startThreads()

    tp.waitForComplete()

    #处理结果

    print 'result Queue\'s length == %d '% tp.resultQueue.qsize()

    while tp.resultQueue.qsize():

        print tp.resultQueue.get()

    print 'end testing'

 

if __name__ == '__main__':

    test()

你可能感兴趣的:(python)