Sep 19, 2019

Python threads using Queue

#Ref:
#https://stackoverflow.com/questions/47900922/split-list-into-n-lists-and-assign-each-list-to-a-worker-in-multithreading
#https://pymotw.com/2/Queue/

#The Queue module provides a FIFO implementation suitable for multi-threaded programming. 
#It can be used to pass messages or other data between producer and consumer threads safely. 
#Locking is handled for the caller, so it is simple to have as many threads as you want working with the same Queue instance. 
#A Queue’s size (number of elements) may be restricted to throttle memory usage or processing.

#### 3 types of queues
# Basic FIFO Queue
# LIFO Queue
# Priority Queue

from queue import Queue, LifoQueue
from threading import Thread, current_thread
from time import sleep
first_names = ['Steve','Jane','Sara','Mary','Jack','tara','bobby']

q = Queue() #FIFO
lq = LifoQueue() #LifoQueue

num_threads = 3

def do_stuff(q):
    while True:
        print(q.get(), current_thread())
        sleep(1)
        q.task_done()

if __name__ == '__main__':
    print('------ FIFO - Basic ------')

    for x in first_names:
        q.put(x)
    
    for i in range(num_threads):
        worker = Thread(target=do_stuff, args=(q,))
        worker.start()

    q.join()
    
    print('------ LIFO - reverse order -----')

    for x in first_names:
        lq.put(x)
    
    for i in range(num_threads):
        worker = Thread(target=do_stuff, args=(lq,))
        worker.start()

    lq.join()



Output:

------ FIFO - Basic ------
Steve <Thread(Thread-1, started 140202362271488)>
Jane <Thread(Thread-2, started 140202353878784)>
Sara <Thread(Thread-3, started 140202345486080)>
Mary <Thread(Thread-1, started 140202362271488)>
Jack <Thread(Thread-2, started 140202353878784)>
tara <Thread(Thread-3, started 140202345486080)>
bobby <Thread(Thread-1, started 140202362271488)>
------ LIFO - reverse order -----
bobby <Thread(Thread-4, started 140202337093376)>
tara <Thread(Thread-5, started 140202328700672)>
Jack <Thread(Thread-6, started 140202320307968)>
Mary <Thread(Thread-4, started 140202337093376)>
Sara <Thread(Thread-5, started 140202328700672)>
Jane <Thread(Thread-6, started 140202320307968)>
Steve <Thread(Thread-4, started 140202337093376)>


No comments:

Post a Comment