Working fifo input now.

This commit is contained in:
kalzu rekku 2024-02-11 12:33:13 +02:00
parent 8f80c98c77
commit b4068ff0de

View File

@ -1,6 +1,8 @@
import queue import queue
import os
import time import time
import threading import threading
import re
def main(): def main():
print("Hello There") print("Hello There")
@ -10,39 +12,60 @@ def main():
config = {'output': '/tmp/test_out', 'input': '/tmp/test_in'} config = {'output': '/tmp/test_out', 'input': '/tmp/test_in'}
#input(que) #input(que)
input_thread = threading.Thread(target=handle_input, args=(que,event,config)) input_thread = threading.Thread(target=handle_input, args=(que, event, config))
input_thread.daemon = True input_thread.daemon = True
input_thread.start() input_thread.start()
#output(que) #output(que)
output_thread = threading.Thread(target=handle_output, args=(que,event,config)) output_thread = threading.Thread(target=handle_output, args=(que, event, config))
output_thread.daemon = True output_thread.daemon = True
output_thread.start() output_thread.start()
for num in range(21): countdown(config, event)
print(f"wait till full: {num}/20", end='\r')
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) time.sleep(0.5)
num = num + 1
print("\nDone!") print("\nDone!")
event.set()
def handle_input(que, event, config): def handle_input(que, event, config):
loop = 0
file = config['input'] file = config['input']
while not event.is_set(): try:
message = f"{time.time()} | Hi! | {file} | {loop}" os.mkfifo(file)
que.put(message) except FileExistsError:
loop = loop + 1 pass
time.sleep(1) 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): def handle_output(que, event, config):
file = '/tmp/test_out' file = config['output']
future_file = config['output']
with open(file, 'a') as out_file: with open(file, 'a') as out_file:
while not event.is_set(): while not event.is_set():
message = que.get() try:
out_file.write(f"{future_file} | {message}\r\n") message = que.get()
out_file.flush() except queue.Empty:
time.sleep(1) 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__": if __name__ == "__main__":