2024-02-11 09:05:43 +02:00
|
|
|
import queue
|
|
|
|
import time
|
|
|
|
import threading
|
|
|
|
|
|
|
|
def main():
|
|
|
|
print("Hello There")
|
|
|
|
|
|
|
|
que = queue.Queue()
|
|
|
|
event = threading.Event()
|
2024-02-11 09:31:37 +02:00
|
|
|
config = {'output': '/tmp/test_out', 'input': '/tmp/test_in'}
|
2024-02-11 09:05:43 +02:00
|
|
|
|
|
|
|
#input(que)
|
2024-02-11 09:31:37 +02:00
|
|
|
input_thread = threading.Thread(target=handle_input, args=(que,event,config))
|
2024-02-11 09:05:43 +02:00
|
|
|
input_thread.daemon = True
|
|
|
|
input_thread.start()
|
|
|
|
|
|
|
|
#output(que)
|
2024-02-11 09:31:37 +02:00
|
|
|
output_thread = threading.Thread(target=handle_output, args=(que,event,config))
|
2024-02-11 09:05:43 +02:00
|
|
|
output_thread.daemon = True
|
|
|
|
output_thread.start()
|
|
|
|
|
|
|
|
for num in range(21):
|
|
|
|
print(f"wait till full: {num}/20", end='\r')
|
|
|
|
time.sleep(0.5)
|
|
|
|
print("\nDone!")
|
|
|
|
event.set()
|
|
|
|
|
2024-02-11 09:31:37 +02:00
|
|
|
def handle_input(que, event, config):
|
2024-02-11 09:05:43 +02:00
|
|
|
loop = 0
|
2024-02-11 09:31:37 +02:00
|
|
|
file = config['input']
|
2024-02-11 09:24:45 +02:00
|
|
|
while not event.is_set():
|
2024-02-11 09:31:37 +02:00
|
|
|
message = f"{time.time()} | Hi! | {file} | {loop}"
|
2024-02-11 09:05:43 +02:00
|
|
|
que.put(message)
|
|
|
|
loop = loop + 1
|
|
|
|
time.sleep(1)
|
|
|
|
|
2024-02-11 09:31:37 +02:00
|
|
|
def handle_output(que, event, config):
|
2024-02-11 09:05:43 +02:00
|
|
|
file = '/tmp/test_out'
|
2024-02-11 09:31:37 +02:00
|
|
|
future_file = config['output']
|
2024-02-11 09:05:43 +02:00
|
|
|
with open(file, 'a') as out_file:
|
2024-02-11 09:24:45 +02:00
|
|
|
while not event.is_set():
|
2024-02-11 09:05:43 +02:00
|
|
|
message = que.get()
|
2024-02-11 09:31:37 +02:00
|
|
|
out_file.write(f"{future_file} | {message}\r\n")
|
2024-02-11 09:05:43 +02:00
|
|
|
out_file.flush()
|
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|
|
|
|
print("Bye now!")
|
|
|
|
exit(0)
|