Editing
Namedpipe Log Writer
From EDURange
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
[[Category:Demonstrations]] See https://github.com/edurange/demo-namedpipe-log-writer/tree/main. This is a demo of using a [https://man7.org/linux/man-pages/man7/fifo.7.html fifo] via the [https://man7.org/linux/man-pages/man1/mkfifo.1.html mkfifo] utility for concurrent IO between two processes, such as a log producer and log consumer. == Instructions == To run it one can use `./[https://github.com/edurange/demo-namedpipe-log-writer/blob/main/toy_start_log.sh toy_start_log.sh]`, which is a very reductive mock of [https://github.com/edurange/edurange3/blob/main/scenarios/global_scripts/start_ttylog.sh start_ttylog.sh]. The commands from toy_start_log.sh are easy to run manually, as well. == Explanation == Previous versions rely heavily on polling and other busy waiting when coordinating between processes. Code that uses appropriate synchronization mechanisms is more performant, and easier to read and maintain. "fifos", also known as "named pipes", are one of the core building blocks of inter-process communication on POSIX/UNIX platforms. In line with UNIX "everything is a file" design thinking, they look just like a file. But they have no persistent storage. They are a temporary first-in first-out queue. Fifos behave differently from files in one other key way. When a reader is reading from a normal file and reaches the end of the data, the filesystem sends an end-of-file character. When the file is a fifo, a read or seek beyond the end of the buffer simply blocks. This is important because many core programming language libraries which implement basic IO with the operating system respond to the EOF character by closing the file and releasing the file descriptor. In a live data pipeline, we know that most of the time when we run out of data, there will be more ready for us very soon. Working around this without fifos takes a lot of effort to monitor the filesystem for a change after the log consumer reaches an EOF character. And that's because the desired behavior actually already exists in the form of this very mundane fossil from the command line. Rather than let the file close in this demo, I use a fifo so that reads always block even when the buffer is empty. Then I wrap this communication in asynchronous reads so that [https://docs.python.org/3/library/asyncio.html asyncio] will respond to the availability of readable data, rather than polling for it. Due to some technical limitations of the Python interpreter (related to the Global Interpreter Lock) it's necessary to wrap the read operation in a thread to make it awaitable by asyncio. That's not a general endorsement of threading, and such details would be wrapped in a production implementation.
Summary:
Please note that all contributions to EDURange may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
EDURange:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Page actions
Page
Discussion
Read
Edit
Edit source
History
Page actions
Page
Discussion
More
Tools
Personal tools
Not logged in
Talk
Contributions
Create account
Log in
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Search
Tools
What links here
Related changes
Special pages
Page information