在这种情况下,如何用信号量或互斥锁替换队列

How can I replace the queue in this case with semaphore or mutex lock

我有 4 个线程从 4 个文本文件中读取,另外有一个线程写入 4 个线程已经读取,我使用了队列,那么如何使用信号量或互斥锁?

import queue                    
import threading                

s = threading.Semaphore(5)
def print_text(s, q, filenames):
    with s:
        for line in open(filenames, encoding="utf8"):
            q.put(line.strip())
        q.put('--end--')

def print_result(q, count=0):   
    while count:                
        line = q.get()          
        if line == '--end--':   
            count -= 1          
        else:
            print(line)

if __name__ == "__main__":
    filenames = ['file_1.txt', 'file_2.txt', 'file_3.txt', 'file_4.txt']

    q = queue.Queue()    

    threads = [threading.Thread(target=print_text, args=(s, q, filename)) for filename in filenames]
    
    threads.append( threading.Thread(target=print_result, args=(q, len(filenames))) ) 

    for thread in threads:
        thread.start()
    
    for thread in threads:
        thread.join()