dogweather

dogweather

Here's a Python async function. What would the Elixir version be like?

I have a function repair_file_in_place(path) that makes HTTP connections to check links for 404 errors.

Here’s the Python code to run repair_file_in_place() concurrently on all the markdown files in a directory:

async def repair_files_in_directory(path: str):
    """
    Repair all markdown files recursively in a directory.
    """
    async with aiohttp.ClientSession() as session:
        async with asyncio.TaskGroup() as group:
            for root, _, files in os.walk(path):
                for file in files:
                    if file.endswith(".md"):
                        group.create_task(repair_file_in_place(os.path.join(root, file), session))
  • How would the code be structured in Elixir / OTP?
  • Is there some kind of semaphore-like ability to limit the concurrency? My use case is not blowing the ulimit on open files.

Marked As Solved

zachallaun

zachallaun

Probably something like:

path
|> Path.join("**/*.md")
|> Path.wildcard()
|> Task.async_stream(&repair_file_in_place/1)
|> Stream.run()

Concurrency can be limited using options passed to async_stream. Docs: Task — Elixir v1.20.2

(Note: untested and possibly not optimal, but this is the first that came to mind!)

Also Liked

zachallaun

zachallaun

This is handled by the wildcard, which expands the glob "your/path/**/*.md" into a list of all the .md files that live anywhere in your/path. :smiling_face:

dimitarvp

dimitarvp

People dig Elixir for a reason, my dude. :smiley:

That kind of expressive and readable code hooked me to Elixir almost 8 years ago.

Where Next?

Popular in Questions Top

aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
mgjohns61585
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from ...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: <h1>Create Post</h1> <%= ...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New

Other popular topics Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New

We're in Beta

About us Mission Statement