Elixir: what is the point of Async HTTP?

I use languages ​​in which request handlers work in the stream, so all I / O functions have an asynchronous version to prevent blocking the stream.

In Elixir, each request is processed in an easy process (actor?), And the runtime can multiplex thousands of participants in one OS thread. If an actor is blocked, the runtime replaces the other actor with processor utilization. Since the actor can block without blocking the stream, I do not see the point of asynchronous functions in Elixir. However, I came across this in the HTTPotion documentation:

iex> HTTPotion.get "http://floatboth.com", [], [stream_to: self] %HTTPotion.AsyncResponse{id: {1372,8757,656584}} 

What is the meaning of the async function here?

+6
source share
1 answer

In README for HTTPotion, using stream_to will send messages to the provided Pid for each HTTP response fragment. You can use the receive unit to receive them and process them accordingly.

In general, it makes no sense to say, "In Elixir, every request is processed ..." because the request means a very specific thing. Assuming this is related to the web application and incoming requests.

In Elixir, each process is a piece of code executed in order. When this piece of code is finished, the process dies. One use of asynchronous responses in HTTPotion can be selective retrieval, where you want to process the material as quickly as possible, but messages matching a specific pattern can take precedence. Selective technique is one of the advantages of how Erlang handles concurrency by the way, for example, Go handles it (CSP).

Hope this is helpful. The fact is that an actor can block without blocking the OS level flow, but sometimes you want this message to take priority, and in this case, selective reception from the mailbox is essential. For example, imagine that one of the possible messages would be equivalent to β€œshucks”, because of this value I know that I’m not interested in this HTTP response at all. "Then you can close the process, and not waste processor processing (previously received, intensive for CPU) messages that have been queued in the mailbox.

+3
source

Source: https://habr.com/ru/post/970098/


All Articles