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 os
import time
import threading
import re
def main():
print("Hello There")
@ -10,39 +12,60 @@ def main():
config = {'output': '/tmp/test_out', 'input': '/tmp/test_in'}
#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.start()
#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.start()
for num in range(21):
print(f"wait till full: {num}/20", end='\r')
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!")
event.set()
def handle_input(que, event, config):
loop = 0
file = config['input']
while not event.is_set():
message = f"{time.time()} | Hi! | {file} | {loop}"
que.put(message)
loop = loop + 1
time.sleep(1)
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 = '/tmp/test_out'
future_file = config['output']
file = config['output']
with open(file, 'a') as out_file:
while not event.is_set():
message = que.get()
out_file.write(f"{future_file} | {message}\r\n")
out_file.flush()
time.sleep(1)
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__":