Skip to content

Instantly share code, notes, and snippets.

@skriticos
Created June 2, 2009 18:10
Show Gist options
  • Save skriticos/122424 to your computer and use it in GitHub Desktop.
Save skriticos/122424 to your computer and use it in GitHub Desktop.
#! /usr/bin/env python3
""" Fifo read/write handler snipet
Opens the file 'thefifo' for reading and writing. Writes something to the
fifo and then enters the read loop. The data written will be recovered
unless some other process is reading the file, as the delay will allow the
other process to read it. (Try '% cat thefifo' before running the script.)\
"""
import os, time, sys, atexit
## exit handler
atexit.register(os.system, 'rm thefifo')
## prepare fifo
if not os.path.exists('thefifo'): os.system('mkfifo thefifo')
## open fifo
fd = os.open('thefifo', os.O_RDWR | os.O_NONBLOCK)
## write to fifo
os.write(fd, b'Some data')
time.sleep(0.1)
## read fifo in an endless loop
print ('>> Entreing read loop, hit ctrl+c to exit..')
while True:
try:
buf = os.read(fd, 4096)
if buf: print (buf)
except:
try: time.sleep(0.1)
except KeyboardInterrupt:
sys.exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment