chatgpt/letters/fifo-sample.py

37 lines
868 B
Python
Executable File

#!/usr/bin/python3
import os
import sys
import queue
import threading
def listen_to_fifo(q):
fifo = "/tmp/my_fifo"
if not os.path.exists(fifo):
os.mkfifo(fifo)
with open(fifo, 'r') as f:
while True:
data = f.readline().strip()
if not data:
break
q.put(data)
def read_queue(q):
while True:
data = q.get()
if data == "reboot":
# Restart the script
print('## RESTARTING SCRIPT')
os.execv(sys.executable, [sys.executable] + sys.argv)
else:
print(data)
sys.stdout.flush()
q.task_done()
if __name__ == '__main__':
q = queue.Queue()
t1 = threading.Thread(target=listen_to_fifo, args=(q,))
t2 = threading.Thread(target=read_queue, args=(q,))
t1.start()
t2.start()
q.join()