Created
November 24, 2021 09:06
-
-
Save kflu/4fd6cd3bd393daaa7636b7aa1742ee52 to your computer and use it in GitHub Desktop.
python non-block opening and reading from fifo (eagerly with select)
This file contains hidden or 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
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