In [1]:
import queue
q = queue.Queue()
for i in range(5):
q.put(i)
while not q.empty():
print(q.get(), end=' ')
print()
In [2]:
import queue
q = queue.LifoQueue()
for i in range(5):
q.put(i)
while not q.empty():
print(q.get(), end=' ')
print()
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()
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()
In [ ]: