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
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.16.0
(Note: untested and possibly not optimal, but this is the first that came to mind!)
9
Also Liked
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. ![]()
2
dimitarvp
People dig Elixir for a reason, my dude. ![]()
That kind of expressive and readable code hooked me to Elixir almost 8 years ago.
1
Popular in Questions
can someone please explain to me how Enum.reduce works with maps
New
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this:
...
New
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
I have an umbrella app.
Some of the apps inside depend on other apps in the umbrella, unsurprisingly.
I'm writing a test for one of t...
New
What is most correct way to open, read and parse JSON file with poison?
For example if we have example.json file in root of some projec...
New
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this
"1000"
What is the ...
New
Hey,
I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
Hi,
I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
Given a string, how can I get access to its character by index? Enum.at("my_string", 2) doesn't work.
Or rather, not char, but a substr...
New
I am trying to run a deploy with docker and I successfully runned with this command:
docker build -t romenigld/blog-prod .
but when I t...
New
Other popular topics
Manning 2016 Halloween weekend sale via Deal of the Day
Friday, October 28 - Half off all MEAPs - code WM102816LT
Saturday, October 29 ...
New
can someone please explain to me how Enum.reduce works with maps
New
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
Good day to you all.
I have been struggling to get a query involving like and ilike to work.
Can anyone assist me on this, please?
pro...
New
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine)
This is a plugin that adds support for Elixir to JetBrains IntelliJ...
New
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
I tried installing
elixir 1.11.2
erlang 23.3.4
via asdf in my zsh shell. Enabled the versions locally and globally.
When I list them ...
New
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
Hi guys, i’m new in the Elixir world, and i have to say, that i love it!
i’m having some problem to understand anonymous functions with ...
New







