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 多线程
最新推荐文章于 2025-07-15 09:23:09 发布
本文介绍了一个使用Python实现的多线程任务处理程序。通过创建自定义线程类并利用队列来分配任务,该程序能够高效地并行处理多个任务。文章详细展示了如何初始化线程、启动线程以及等待所有任务完成。
3408

被折叠的 条评论
为什么被折叠?



