import queue import os import time import threading import re def main(): print("Hello There") que = queue.Queue() event = threading.Event() config = {'output': '/tmp/test_out', 'input': '/tmp/test_in'} #input(que) input_thread = threading.Thread(target=handle_input, args=(que, event, config)) input_thread.daemon = True input_thread.start() #output(que) output_thread = threading.Thread(target=handle_output, args=(que, event, config)) output_thread.daemon = True output_thread.start() countdown(config, event) def countdown(config, event): print(f"Speak to: {config['input']}") print(f"Output is going to be: {config['output']}") num = 0 while not event.is_set(): print(f"wait till full: {num}/~", end='\r') time.sleep(0.5) num = num + 1 print("\nDone!") def handle_input(que, event, config): file = config['input'] try: os.mkfifo(file) except FileExistsError: pass with open(file, 'r') as in_fifo: while not event.is_set(): fifo_read = in_fifo.readline().rsplit('\n') if not fifo_read: continue message = fifo_read que.put(message) time.sleep(1) def handle_output(que, event, config): file = config['output'] with open(file, 'a') as out_file: while not event.is_set(): try: message = que.get() except queue.Empty: continue if message == "": continue elif 'EXIT' in message: event.set() out_file.write(f"{time.time()} | Bye!\r\n") out_file.flush() else: out_file.write(f"{time.time()} | {message[0]}\r\n") out_file.flush() time.sleep(1) if __name__ == "__main__": main() print("Bye now!") exit(0)