nathanl

nathanl

When blocking is beautiful

I’m working on a DockYard blog post idea and would like some input / examples from your experience.

Because it uses Cowboy, Phoenix spawns one cheap process per incoming request, and these are pre-emptively scheduled. This means that doing blocking work in one request doesn’t interfere with other requests, unlike in web frameworks where you have a limited number of server threads available. So the decision of whether to block vs use a background task is purely about user experience, not about keeping your site up.

Being able to block can let you do things more simply. Eg, import an uploaded CSV in a normal request cycle. Could be a background job, but doesn’t have to be.

It can also allow things that would be hard / impossible otherwise. Eg, in one project I know of, there are encrypted images in S3 that can’t be referenced directly from an <img> tag because the browser won’t be able to access them, so those image requests get proxied through Phoenix, which has access and can send down the unencrypted data. The client doesn’t even need to know the S3 bucket or key.

Have you seen other examples where being able to do blocking work in the request cycle is helpful?

Most Liked

tmbb

tmbb

You have to be very careful with this. If you call a function from the controller action, and the user disconnects from the server while the function runs, the function will be interrupted. You should run long-running tasks under a different sueprvised process so that things keep working on the server even if the client disconnects. The Task module contains functions that help you do this and retrieve the result as if everything were aynchronous

nathanl

nathanl

@tmbb I relayed your point to @chrismccord and Jason Goldberger, and they pointed out that you could async_nolink a task and block the controller response until you got a result; that doesn’t have the simplicity of “just do it inline”, but it does mean that for the client, getting the response is as simple as “request and wait for the response”.

Nicd

Nicd

I think you mean pre-emptively scheduled, otherwise blocking would be very bad indeed. :slight_smile:

I have one API route that takes the user’s data and generates a big CSV file out of it. It can take a long time and be CPU intensive. I like that I don’t have to fork it out to a background task, but instead I can just handle it in the request directly. The client sets a longer timeout and shows a progress indicator for that route to take care of the user experience.

Where Next?

Popular in Discussions Top

Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
thojanssens1
It would be nice to be able to define a redirect from one route to another from the router.ex file. E.g.: redirect "/", UserController, ...
New
mmport80
I have put far too much effort into Dialyzer over the last year or so - and basically - I doubt it’s worth the effort. It’s not as easy ...
New
chuck
Let me start by stating an assumption: Phoenix is a great approach to building REST APIs. There are many reasons for this, but I will ass...
New
praveenperera
How We Replaced React with Phoenix By: Thought Bot
New
tmbb
This is a post to discuss the new Phoenix LiveView functionality. From Chris’s talk, it appears that they generate all HTML on the serve...
342 18146 126
New
fireproofsocks
I’ve been working on an Elixir project that has required a lot of scripting. I usually reach for Elixir because I like it more (and in th...
New
jesse
Hi everyone, I hesitated to post this here because I don’t want you to think I’m spamming, but I’ve been working on a Platform-as-a-Serv...
New
griffinbyatt
Sobelow Sobelow is a security-focused static analysis tool for the Phoenix framework. For security researchers, it is a useful tool for g...
New
paulanthonywilson
I like Umbrella projects and pretty much always use them for personal Elixir stuff, especially Nerves things. But I don’t think this is ...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39297 209
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New

We're in Beta

About us Mission Statement