Ayoush

Ayoush

Why the consumer constantly send demand upstream in one condition and not in others?

So in the example given here :-GenStage – gen_stage v0.14.0 that of A → B → C I implemented that and saw that the consumer is able to constantly send demands upstream and the numbers are getting printed. But when I created a normal consumer, producer

defmodule Asyncgenstage.Producer do
  use GenStage
  def start_link(_args)  do
    GenStage.start_link(__MODULE__, :ok, name: __MODULE__)
  end

  def init(args) do
    with {:ok, pid} <- :python.start([{:python_path, to_charlist("path")},{:python, 'python3'}]) do
      {:producer, pid}
    end
  end

  def handle_demand(demand, state) do
    result = call_python(state)
    {:noreply, result, state}
  end

  def call_python(pid) do
    call_MFA(pid)
  end

  defp call_MFA(pid) do
    module = :url_generator
    function = :main
    arguments = [10]
    :python.call(pid, module, function, arguments)
  end

end

defmodule Asyncgenstage.Consumer do
  use GenStage
  alias Asyncgenstage.Producer, as: Producer
  def start_link, do: start_link([])
  def start_link(_args) do
    GenStage.start_link(__MODULE__, :ok, name: __MODULE__)
  end

  def init(:ok) do
    {:consumer, :ok, subscribe_to: [{Producer, max_demand: 10, mid_demand: 1}]}
  end


  def handle_events(events, from, state) do
    IO.inspect(events)
    {:noreply, [], state}
  end
end

I observer the demand is not getting send continuously so I thought maybe the the function is not getting called so many times but when I introduced max_demand and min_demand in consumer then it start sending demand upstream constantly. Why is that, max_demand and min_demand are just opts right like by default also we have that value then why on explicitly declaring them is triggering the demand upstream constantly but without them its not.

Marked As Solved

LostKobrakai

LostKobrakai

You want to read the following part in the docs of a recent version of GenStage: GenStage — gen_stage v1.2.1

It explains how demand is handled. In your case the problem is likely the producer, which produces a fixed amount of events whenver handle_demend is triggered instead of producing as many events as there has been demand sent.

Your producer is expected to produce 5* events eventually if it received a demand of 5 in handle_demand. Consumers won’t demand more before their previous demand has been fulfilled to some degree (depending on min and max_demand setting).

[*] At least 5. GenStage can buffer some events exceeding demand to make e.g. producers working with batches of data simpler.

Where Next?

Popular in Questions Top

lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
jerry
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
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
Kagamiiiii
Student &amp; New to elixir. Nice language. I want to convert a english character, e.g. “a”, which is stored in a variable, to it’s asci...
New
Phillipp
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
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
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

Other popular topics Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
jerry
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
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
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
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
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; someth...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement