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.68k
    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-

    #!/usr/bin/python
    
    import Queue
    import threading
    import time
    
    exitFlag = 0
    
    class myThread (threading.Thread):
        def __init__(self, threadID, name, q):
            threading.Thread.__init__(self)
            self.threadID = threadID
            self.name = 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:
            queueLock.acquire()
            if not workQueue.empty():
                data = q.get()
                queueLock.release()
                print "%s processing %s" % (threadtest, data)
            else:
                queueLock.release()
            time.sleep(1)
    
    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
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
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: