Python Thread Pooling


import queue
import threading
import time
#TaskProcesser is my own module which is processing task, write your own module or function which provide job to thread.
import TaskProcesser

tasklist = [
"task1", 
"task2", 
"task3", 
"task4", 
"task5" 
]

taskqueue = queue.Queue()

class ThreadTask(threading.Thread):
def __init__(self, taskqueue):
threading.Thread.__init__(self)
self.taskqueue = taskqueue

def run(self):
while True:
start = time.time()
#get task from taskqueue
task = self.taskqueue.get()
#do processing of task
TaskProcesser.doTask(task)
#inform taskqueue about job has done
self.taskqueue.task_done()
print("Task compeleted in : " +str(time.time() - start) + " seconds")
def main():

#create thread pool and pass taskqueue
for i in range(2):
t = ThreadUrl(taskqueue)
t.setDaemon(True)
t.start()

#populate taskqueue with task   
for task in tasklist:
taskqueue.put(task)

#wait for queue, til task processing over 
taskqueue.join()

main()

No comments:

Post a Comment

would you like it. :)