Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
  • Multithreaded Priority Queue in Python

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.65k
    Comment on it

    With the help of Queue module we can create a new queue object that can hold a specific number of items and threads. Queue module have following method which is given below.
    get(): The get() removes and returns an item from the queue.
    put(): The put adds item to a queue.
    qsize() : The qsize() returns the number of items that are currently in the queue.
    empty(): The empty( ) returns True if queue is empty; otherwise, False.
    full(): the full() returns True if queue is full; otherwise, False.
    And example of all method is given below-

    import Queue
    import threading
    import time
    exitFlag = 0
    class myThread (threading.Thread):
        def __init__(self, threadID, name, q):
            self.threadID = threadID
   = name
            self.q = q
        def run(self):
            print "Starting " + self.test
            process_data(self.test, self.q)
            print "Exiting " + self.test
    def process_data(threadName, q):
        while not exitFlag:
            if not workQueue.empty():
                data = q.get()
                print "%s processing %s" % (threadtest, data)
    threadList = ["Demo-1", "Demo-2", "Demo-3"]
    nameList = ["One", "Two", "Three", "Four", "Five"]
    queueLock = threading.Lock()
    workQueue = Queue.Queue(10)
    threads = []
    threadID = 1

 0 Comment(s)

Sign In

Sign up using

Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: