Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elixir: what's the point of Async HTTP?

I'm used to languages where the request handlers run on a thread, so all I/O functions have an async version to prevent blocking the thread.

In Elixir, each request is handled in a lightweight process (actor?), and the runtime can multiplex thousands of actors on a single OS thread. If an actor blocks, the runtime swaps another actor to use the cpu. Since, an actor can block without blocking the thread, I don't see the point of async functions in Elixir. Yet, I came across this in the HTTPotion documentation:

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

What's the point of an async function here?

like image 606
tldr Avatar asked May 30 '14 15:05

tldr


1 Answers

Per the README for HTTPotion, using stream_to will cause messages to be sent to the provided Pid for each chunk of the http response. You could use a receive block to accept these and handle them accordingly.

In general, it doesn't make sense to say "In Elixir, each request is handled..." because request means a very specific thing. Assuming this is related to a webapp and inbound requests.

In Elixir, each process is a chunk of code executed in order. When that chunk of code is finished, the process dies. One use of async responses in HTTPotion could be selective receive, where you want to process stuff as fast as possible but messages matching a certain pattern might take precedence. Selective Receive is one of the benefits of how Erlang handles concurrency over how, for instance, Go handles it (CSP).

Hope this is helpful. The point is, an actor can block without blocking the OS-level thread, but sometimes you want a given message to take priority, and in that case selective-receive out of the mailbox has substantial value. For instance, imagine one of the possible messages would be equivalent to "shucks, because of this value I know I don't care about this http response at all." Then you could shut down the process and not waste CPU processing the (earlier-received, CPU-intensive) messages that had queued in the mailbox.

like image 125
Josh Adams Avatar answered Sep 20 '22 10:09

Josh Adams