1 Basic FIFO Queue


In [1]:
import queue
q = queue.Queue()
for i in range(5):
    q.put(i)
while not q.empty():
    print(q.get(), end=' ')
print()


0 1 2 3 4 

2 LIFO Queue

behave like stack


In [2]:
import queue
q = queue.LifoQueue()
for i in range(5):
    q.put(i)
while not q.empty():
    print(q.get(), end=' ')
print()


4 3 2 1 0 

2 Priority Queue


In [8]:
import functools
import queue
import threading
@functools.total_ordering
class Job:

    def __init__(self, priority, description):
        self.priority = priority
        self.description = description
        print('New job:', description,'\n')
        return

    def __eq__(self, other):
        try:
            return self.priority == other.priority
        except AttributeError:
            return NotImplemented

    def __lt__(self, other):
        try:
            return self.priority < other.priority
        except AttributeError:
            return NotImplemented

In [10]:
q = queue.PriorityQueue()

q.put(Job(3, 'Mid-level job'))
q.put(Job(10, 'Low-level job'))
q.put(Job(1, 'Important job'))


def process_job(q):
    while True:
        next_job = q.get()
        print('Processing job:', next_job.description,'\n')
        q.task_done()


workers = [
    threading.Thread(target=process_job, args=(q,)),
    threading.Thread(target=process_job, args=(q,)),
]
for w in workers:
    w.setDaemon(True)
    w.start()

q.join()


New job: Mid-level job 

New job: Low-level job 

New job: Important job 
Processing job:
 Processing job: Important jobMid-level job  



Processing job: Low-level job 

Producer and Consumer


In [15]:
import functools
import queue
import threading
import random
import time 


@functools.total_ordering
class Job:

    def __init__(self, priority, description):
        self.priority = priority
        self.description = description
        print('New job:', description,'\n')
        return

    def __eq__(self, other):
        try:
            return self.priority == other.priority
        except AttributeError:
            return NotImplemented

    def __lt__(self, other):
        try:
            return self.priority < other.priority
        except AttributeError:
            return NotImplemented

q = queue.PriorityQueue()

def producer(q, cnt):
    while cnt:
        priority = random.randint(10, 100)
        time.sleep(random.random())
        description = 'with priority is :' + str(priority)
        q.put(Job(priority, description))
        cnt -= 1
def consumer(q):
    while true:
        job = q.get()
        print('Processing job',job.description)

producer_t = threading.Thread(target=producer, args=(q, 10,))
workers = [
    threading.Thread(target=consumer, args=(q,)),
    threading.Thread(target=consumer, args=(q,))
]
producer_t.start()
for t in workers:
    t.start()


Exception in thread Thread-18:
Traceback (most recent call last):
  File "/Users/gaufung/anaconda/lib/python3.6/threading.py", line 916, in _bootstrap_inner
    self.run()
  File "/Users/gaufung/anaconda/lib/python3.6/threading.py", line 864, in run
    self._target(*self._args, **self._kwargs)
  File "<ipython-input-15-ab6c1d908f6b>", line 39, in consumer
    while true:
NameError: name 'true' is not defined

Exception in thread Thread-19:
Traceback (most recent call last):
  File "/Users/gaufung/anaconda/lib/python3.6/threading.py", line 916, in _bootstrap_inner
    self.run()
  File "/Users/gaufung/anaconda/lib/python3.6/threading.py", line 864, in run
    self._target(*self._args, **self._kwargs)
  File "<ipython-input-15-ab6c1d908f6b>", line 39, in consumer
    while true:
NameError: name 'true' is not defined

New job: with priority is :64 

New job: with priority is :80 

New job: with priority is :45 

New job: with priority is :44 

New job: with priority is :48 

New job: with priority is :85 

New job: with priority is :35 

New job: with priority is :61 

New job: with priority is :54 

New job: with priority is :47 


In [ ]: