nightfury17200

nightfury17200

hello all, I want to operate on large amount of data where I pass a query to get some records and then for each record I have to perform a costly operation (creating large number of redis keys(~1000) for each record data and then deleting it) so here I want to use a stream to make the strain less. My code till now is-

      Repo.transaction(
        fn ->
          query
          |> Repo.stream(max_rows: @query_batch)
          |> Stream.chunk_every(@query_batch)
          |> Stream.each(fn x ->
             Enum.to_list(x) |> Enum.each(fn y -> delete_from_redis(y) end)
          end)
          |> Enum.to_list()
        end,
        timeout: :infinity
      )

here in this code when I tried to do -

Stream.each(x, fn y -> delete_from_redis(y) end)

in place of-

Enum.to_list(x) |> Enum.each(fn y -> delete_from_redis(y) end)

my code did not worked, please explain the reason for that also please tell me what can I do to make my current code more efficient?

Showing Posts 1 to 9

kokolegorille

kokolegorille

What was the error message?

nightfury17200

nightfury17200 OP

no error message the code just did nothing.

al2o3cr

al2o3cr

Re: your specific question:

Stream.each doesn’t run any computation, you have to call a function like Enum.to_list or Stream.run to make Stream do things.

A more general thought: this code returns all the rows from query which neutralizes a lot of the benefit from streaming; Stream.run will do the computation but ignore the result, if that’s your intent.

nightfury17200

nightfury17200 OP

I read about what stream.run do and what the documentation mentioned was-

Runs the given stream.

This is useful when a stream needs to be run, for side effects, and there is no interest in its return result.

to be honest I was not able to make any sense of it hence I tried using stream.each and thought it would be similar to enum.each and would call a function for each value. Can you please explain how stream.run works?

nightfury17200

nightfury17200 OP

I tried using stream.run assuming that it makes all the stream command inputted in it work. the code I tried is-

      Repo.transaction(
        fn ->
          query
          |> Repo.stream(max_rows: @query_batch)
          |> Stream.chunk_every(@query_batch)
          |> Stream.each(fn x ->
             Stream.each(x, fn y -> delete_from_redis(y) end)
          end)
          |> Stream.run()
        end,
        timeout: :infinity
      )

but it does not work. can you point out where I am making the mistake?

al2o3cr

al2o3cr

Stream.each returns a new stream that calls the supplied function as each element is generated. It doesn’t iterate by itself.

Example:

iex(1)> a = [1,2,3]
[1, 2, 3]

iex(2)> stream = Stream.each(a, &IO.inspect/1)
#Stream<[enum: [1, 2, 3], funs: [#Function<38.58486609/1 in Stream.each/2>]]>

iex(3)> Stream.run(stream)
1
2
3
:ok

iex(4)> Enum.to_list(stream)
1
2
3
[1, 2, 3]

Note that in the second line, stream is bound as a Stream struct but nothing has been IO.inspect-ed yet.

Stream.run causes the stream to compute values but discards them and returns :ok, while Enum.to_list accumulates and returns the values.

nightfury17200

nightfury17200 OP

then shouldn’t the code above your reply run here I have added Stream.run. so first I will make chunks of data with Stream.chunk_every(@query_batch) and then pass it to Stream.each for going to each chunk and then for each chunk I will pass it to Stream.each so that I can iterate over the records and then call the required function for each record, and lastly all of this is passed to stream.run() to implement this. Sorry If my understanding is incorrect.

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Your overall stream does, but you’re doing another Stream.each inside a Stream.each and that inner one isn’t doing anything:

          |> Stream.each(fn x ->
             Stream.each(x, fn y -> delete_from_redis(y) end)
          end)

The inner Stream.each should probably be an Enum.each since the x list is already loaded into memory, streaming here doesn’t accomplish anything.

nightfury17200

nightfury17200 OP

ok, understood. thanks

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews