ijunaidfarooq
I am downloading images through HTTP request with which I am getting a binary image, and writing it to a file such as
File.write(image_with_dir, image, [:binary]) |> File.close
this whole operation of getting HTTP request and then writing it to disk is done in
|> List.flatten()
|> Enum.sort()
|> Task.async_stream(&(inline_process.(&1, images_directory)), max_concurrency: System.schedulers_online() * 2, timeout: :infinity)
|> Stream.run
When decreasing max_concurrency the process got slow approx 2 minutes, also results of System.schedulers_online() is 8
but with current max_concurrency it faster but with this. Disk IO starts touching the limits

Purpose of writing those files is to send them to Dropbox with a batch of 1000 as dropbox upload session supports 1000 images at a time.
Is there any better way to write images to disk? maybe in memory but I don’t know, any help would be wonderful also this operation is being done on Cuda GPU machine but I am not sure how I can use GPU for such purpose.
This process is user defined. user can ask for less/more than 1000 images and those can be one or multiple Task.async_stream’s..
I want to save them on disk so if the process broke or application gets a restart, I can resume the download from where it left.
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #ai
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming











Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
idi527
Could How to download big files? - #3 by yurgon be helpful maybe?
ijunaidfarooq
Actually its about writing them to disk efficiently
idi527
The answer handles that part as well.
dimitarvp
Open the destination file in raw mode with a big buffer (512KB at least, if not a few megabytes even). This also avoids the file operations going through a single internal process – this happens to all non-raw-opened files.
ijunaidfarooq
Okay then I coudnt find in that link
ijunaidfarooq
How to open a file in raw mode? and with a big buffer as well?
idi527
You can try
File.open(filename, [:append, {:delayed_write, buffer_size, delay}])and then write to it withIO.binwrite, the post I linked above describes the last part better.dimitarvp
Try with the the standard File.open docs.
And yeah, @idi527 is right.
dimitarvp
Since I am back at my machine:
Which basically gives you a much faster writing speed, utilising a generous 512KB buffer and 2 seconds of potential delay when the data will eventually make it to the disk (which should be plenty enough even on slow-ish servers).
From then on you can use
IO.writeorIO.binwriteon the returned handle. And don’t forget to callFile.closeat the end!idi527
A file opened in
rawmode wouldn’t work withIO.writeandIO.binwrite, I think?:file.write can be used instead.
Also there are some ownership complications similar to sockets where only the process that opened the file can write to it, not sure if it’s a problem for the
Task.async_streamapproach described in OP (it shouldn’t be a problem if the file is opened and worked with within the same task process).