Skip to content

Instantly share code, notes, and snippets.

@kflu
Created November 24, 2021 09:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kflu/4fd6cd3bd393daaa7636b7aa1742ee52 to your computer and use it in GitHub Desktop.
Save kflu/4fd6cd3bd393daaa7636b7aa1742ee52 to your computer and use it in GitHub Desktop.
python non-block opening and reading from fifo (eagerly with select)
In [93]: import os
In [94]: fd = os.open('fifo', os.O_RDONLY | os.O_NONBLOCK)
In [95]: f = os.fdopen(fd, 'rb')
In [96]: import select
In [97]:
...: while True:
...: r,w,e = select.select([f], [], [f], 0.01)
...: if r:
...: print(f"== data available")
...: print(f"== data: {f.read()}")
...: else:
...: stat = os.fstat(f.fileno())
...: if stat.st_nlink == 0:
...: print("== file is deleted!")
...: break
...:
...:
== data available
== data: b'kkk\n'
== file is deleted!
In [98]:
on the other shell window:
mkfifo fifo
echo kkk >fifo
rm fifo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment