How is reading sockets different from reading files?

I was only planning to rewrite some python code in which I was looking at the file for changes. I wanted to rewrite it as an exercise for asyncio

, and the concept idea was to read a non-blocking file to be displayed. Once the data is available, the event loop will continue executing the coroutine.

Then I discovered that async file operations are not what it does. ref.

But I couldn't figure out what the motivation for this behavior is and how it might differ from sockets.

Socket example:

Reading the socket exits the coroutine until the data is ready. Willingness means that it actually arrived at non-deterministic time from somewhere on the Internet.

Why not read the file:

Reading a file is output from the coroutine until the data is ready. ready this means that it really arrived at non-deterministic time from somewhere on the computer's hard drive

  • Is this legacy behavior from legacy code that works well enough with blocking calls?

  • Is it something to do with symbol and block files?

  • What about character device files, say a file representing a UART connection? Will no file be applied here too?

+3


source to share


1 answer


Definitely not a complete answer, but some thoughts that are too much for comment.

  • Asynchronous programming was originally most useful on networked systems / sockets. While one very rarely has 100k open files and wants to read from them asynchronously, chat servers (or others that handle mostly idle connections) may well have 100k + of these connections. Of course, asynchronous programming is still a "style" that avoids many of the problems with threading programming, but that's not where it started (although I have 0 proofs of this assertion).
  • In the case of files, when someone asks for information, in the future they should arrive at some point. Perhaps this is comparable to making an HTTP request, where you are waiting for a response and you can just wait for it synchronously. On the other hand, a socket can only be opened for push messages, where there is no expectation when (if ever) a message will come. For some special files this may also be true, but I would expect another asynchronous message to be available in this case, after which a synchronized read should occur (e.g. iNotify for regular files, special files have never been viewed).
  • I would say that if you don't know what you are doing, it is actually a very bad idea for massively concurrent file access. Sockets can be useful in this way, since you can connect to different machines. While doing massively parallel file I / O, perhaps you want the OS to serialize the requests for you anyway (have you ever tried to copy two large files from a CD at the same time;))?


As I said, no definite answer, just some thoughts on the matter

+1


source







All Articles