python实现简单的生产者消费者代码

import threading, time
from Queue import Queue

q = Queue()
num = 0
THREAD_NUM=3

class Task():
   def __init__(self, test):
   self.test = test

class HandleTask(threading.Thread):
   global num
   def __init__(self, t_name, queue):
   threading.Thread.__init__(self, name=t_name)
   self.data=queue
   global num
   num = num + 1

   def run(self):
   while(1):
   do something               
   print "%s: %s finished!" %(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())), self.getName())


def handle():
   for i in range(10):
   a = Task("test")
   q.put(a)

   while(num < THREAD_NUM):
   handletask = HandleTask("AAAA", q)
   handletask.start()
   print num


python实现的简单的生产者消费者模型