<jupyter_start><jupyter_text>1 Basic FIFO Queue<jupyter_code>import queue
q = queue.Queue()
for i in range(5):
    q.put(i)
while not q.empty():
    print(q.get(), end=' ')
print()<jupyter_output>0 1 2 3 4<jupyter_text>2 LIFO Queuebehave like stack<jupyter_code>import queue
q = queue.LifoQueue()
for i in range(5):
    q.put(i)
while not q.empty():
    print(q.get(), end=' ')
print()<jupyter_output>4 3 2 1 0<jupyter_text>2 Priority Queue<jupyter_code>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
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()<jupyter_output>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<jupyter_text>Producer and Consumer<jupyter_code>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()<jupyter_output>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
{'fraction_non_alphanumeric': 0.11961722488038277, 'fraction_numerical': 0.018872939925571503, 'mean_word_length': 3.22334455667789, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '620706', 'license': 'MIT', 'n_tokens_mistral': 1284, 'n_tokens_neox': 1223, 'n_words': 309, 'path': 'Python-Standard-Library/DataStructures/queue.ipynb', 'repo_name': 'gaufung/CodeBase', 'size': '3762'}