Python 多线程

来源:互联网 发布:编程实现快速排序算法 编辑:程序博客网 时间:2024/06/08 05:10
import sysimport osimport timeimport threadingimport Queueimport multiprocessingdef ProcessFunction(job):    print jobclass WorkerThread(threading.Thread):    def __init__(self, jobQueue):        threading.Thread.__init__(self)        self.daemon = True        self.__jobQueue = jobQueue    def run(self):        while True:            if self.__jobQueue.empty(): break            ProcessFunction(self.__jobQueue.get())            self.__jobQueue.task_done()            print "Remaining tasks: ", self.__jobQueue.qsize()def RunWithMultithreading(inputJobs):    jobQueue = Queue.Queue()    for job in inputJobs:        jobQueue.put(job)    for x in range(multiprocessing.cpu_count()):        WorkerThread(jobQueue).start()    jobQueue.join()if __name__ == '__main__':    RunWithMultithreading(["job1", "job2", "job3"])
0 0
原创粉丝点击