Python multithreaded priority queue

From , 3 Years ago, written in Python, viewed 88 times.
URL https://pastebin.vip/view/b56b7c12
  1. #!/usr/bin/python
  2.  
  3. import Queue
  4. import threading
  5. import time
  6.  
  7. exitFlag = 0
  8.  
  9. class myThread (threading.Thread):
  10.     def __init__(self, threadID, name, q):
  11.         threading.Thread.__init__(self)
  12.         self.threadID = threadID
  13.         self.name = name
  14.         self.q = q
  15.     def run(self):
  16.         print "Starting " + self.name
  17.         process_data(self.name, self.q)
  18.         print "Exiting " + self.name
  19.  
  20. def process_data(threadName, q):
  21.     while not exitFlag:
  22.         queueLock.acquire()
  23.         if not workQueue.empty():
  24.             data = q.get()
  25.             queueLock.release()
  26.             print "%s processing %s" % (threadName, data)
  27.         else:
  28.             queueLock.release()
  29.         time.sleep(1)
  30.  
  31. threadList = ["Thread-1", "Thread-2", "Thread-3"]
  32. nameList = ["One", "Two", "Three", "Four", "Five"]
  33. queueLock = threading.Lock()
  34. workQueue = Queue.Queue(10)
  35. threads = []
  36. threadID = 1
  37.  
  38. # Create new threads
  39. for tName in threadList:
  40.     thread = myThread(threadID, tName, workQueue)
  41.     thread.start()
  42.     threads.append(thread)
  43.     threadID += 1
  44.  
  45. # Fill the queue
  46. queueLock.acquire()
  47. for word in nameList:
  48.     workQueue.put(word)
  49. queueLock.release()
  50.  
  51. # Wait for queue to empty
  52. while not workQueue.empty():
  53.     pass
  54.  
  55. # Notify threads it's time to exit
  56. exitFlag = 1
  57.  
  58. # Wait for all threads to complete
  59. for t in threads:
  60.     t.join()
  61. print "Exiting Main Thread"
  62.  
  63. #//python/8383

Reply to "Python multithreaded priority queue"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website