Closed smahi closed 3 years ago
The ID identifies a process under the supervisor but the process may still have its own name, which should also be unique. My guess is that the Scrapper.PageConsumer
defines a :name
under its start_link
that is being used. You most likely want something like this:
Supervisor.child_spec({Scrapper.PageConsumer, name: :consumer_a}, id: :consumer_a),
Supervisor.child_spec({Scrapper.PageConsumer, name: :consumer_b}, id: :consumer_b)
If you share the code for PageConsumer I can provide more detailed feedback. :)
Thank you so much for your help @josevalim This is the code of consumer module
defmodule Scrapper.PageConsumer do
use GenStage
require Logger
alias Scrapper.PageProducer
def start_link(_args) do
GenStage.start_link(__MODULE__, [], name: __MODULE__)
end
def init(initial_state) do
Logger.info("Page Consumer init...")
{:consumer, initial_state, subscribe_to: [{PageProducer, min_demand: 0, max_demand: 1}]}
end
def handle_events(events, _from, state) do
Logger.info("Page Consumer received #{inspect(events)}")
events
|> Enum.each(fn _page ->
Scrapper.work()
end)
{:noreply, [], state}
end
end
Yes, the issueis here:
def start_link(_args) do
GenStage.start_link(__MODULE__, [], name: __MODULE__)
end
Do this:
def start_link(opts) do
GenStage.start_link(__MODULE__, [], name: opts[:name])
end
And change the supervisor code to the one in the previous comment and you should be good!
@josevalim It worked like charm. By the way I am reading a book called Conccurent Data Processing by Svilen Gospodinov published by The Pragmatic Bookshelf, I took my code from the book examples.
Thank you so much @josevalim
I am using
GenStage
to create data processing pipeline. With a single consumerPageConsumer
it works fine, but not with multiple processes for the same consumer modulePageConsumer
, instead it raises the following error.Environment
ubuntu 20.04
Current behavior
$ iex -S mix
Raises the following Error:
Expected behavior
Launch two processes
:consumer_a
and:consumer_b