Python 多线程

import sys
import os
import time
import threading
import Queue
import multiprocessing

def ProcessFunction(job):
    print job

class 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"])

你可能感兴趣的:(Python)