Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Multithreading Queue

import threading
from queue import Queue



print_lock = threading.Lock()
def job(worker):
    with print_lock:
        with open('messages.txt') as f:
            for line in f:
                print(line)

def reader():
    while True:
        worker = q.get()
        job(worker)
        q.task_done()

q = Queue()

for x in range(10):
    t = threading.Thread(target=reader)

    t.daemon = True
    t.start()


for worker in range(1):
    q.put(worker)

q.join()

So what i want is each thread reads different messages,

like image 433
Grey Byte Avatar asked Oct 20 '25 09:10

Grey Byte


1 Answers

Queue is thread safe

so, threadling lock does not needed

like image 140
yixing yan Avatar answered Oct 22 '25 00:10

yixing yan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!