Created
June 2, 2009 18:10
-
-
Save skriticos/122424 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /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