What is non-blocking I / O?

9
  • What is non-blocking I / O ?
  • What are the uses of a language with non-blocking I / O ?
  • What are the practical applications of non-blocking I / O ?

It certainly does not get into opinion issues, so I wanted to know the possible issues.

    
asked by anonymous 21.02.2017 / 16:33

1 answer

9
  

What is non-blocking I / O?

It is the ability to do input and output operations ( access file system, database, network, servers, etc.) without the application being prevented from performing other things in parallel.

It is very common for input and output to be "pretty" because of the hardware that controls this operation, but it almost does not consume processing, it would be a waste not to let the application do other things, so some can be applied to parallelize execution , each meeting a different need. The most popular currently is asynchronicity .

Blocking applications are especially bad when you are interacting with the user.

There are cases that even the operation performed on a server can be impaired by the lock and make the user receive the delayed information.

There may be operations that block opposition. Imagine an application waiting for the user to type something. It is usually a blocking operation. But it does not have to be, there is how to allow other operations to be performed in parallel, the so-called "in background .".

This is generally beneficial in improving the user experience .

  

What are the uses of a language with non-blocking I / O?   What are the practical applications of non-blocking I / O?

This feature is just a specific point of the language. It often has more to do with the library than with language.

Any operation at the moment that there is interaction directly with the user, whether by console, GUI, browser, etc., if it blocks the user from doing other tasks, it can not even use the keyboard. What's more, the system may stop responding, and not only can the user try to close it, but even the operating system can do this preventively.

So if you're going to do a longer operation, make it non-blocking, so the moment the operation is waiting for the I / O hardware response the CPU remains free to do other things.

If you loop expecting the answer, it will probably be blocking. Creating an event and notification system ( default Observer ) saves a lot of processing. The code says that you want to be notified when the operation ends and you have an answer, so you do not have to keep repeating the question if you already have an answer or not. It's a lot smarter.

Shape by loop:

pede pra fazer algo
tá pronto?
tá pronto?
tá pronto?
tá pronto?
tá pronto?
tá pronto?
tá pronto?
tá pronto?
.
.
.
em algum momento responde "sim" e continua daqui

Smart code:

faça seu trabalho e me avise quando terminar -> (outra operação demorada executa, notifica)
terminou, continuo daqui

Look at the difference in work.

With this the processor does not work to control the loop and is free to meet other demands. Obviously only doing this does not parallelize anything, the parallel can occur with processes, threads, or some control of the language or library such as polling event flags > green , signals , green , callback functions , channels , >.

In language servers that have a mechanism for not blocking execution, it can greatly facilitate the execution of multiple concurrent requests without wasting resources because it is blocked or managing the competition, resulting in much greater responsiveness.

Especially in web server it is common to make more I / O access than processing, there is an advantage in doing so, which is why almost all modern languages (some or even so modern) have mechanisms in themselves or in the library that allows non-blocking operations.

21.02.2017 / 17:16