#Spawning multiple processes

1 messages · Page 1 of 1 (latest)

flat marsh
#

Hello everyone!

So I'm trying to wrap my head around GenStage pipelines, and although tutorial code works flawlessly, when I try to change the logic to my need suddenly the process spawning under the supervision tree breaks. Here's my Application.start/2

def start(_type, _args) do
    IO.puts("Starting up")
    import Supervisor.Spec, warn: false

    children = [
      {PermitsProcessor.Producer, read_file()}
    ]

    producer_consumers =
      for id <- 1..System.schedulers_online() do
        Supervisor.child_spec(
          {PermitsProcessor.ProducerConsumer, []},
          id: :"ProducerConsumer-#{id}"
        )
      end

    consumers =
      for id <- 1..System.schedulers_online() do
        Supervisor.child_spec(
          {PermitsProcessor.Consumer, []},
          id: :"Consumer-#{id}"
        )
      end

    children = children ++ producer_consumers ++ consumers

    opts = [strategy: :rest_for_one, name: PermitsProcessor.Supervisor]
    Supervisor.start_link(children, opts)
  end

Now, regardless if I use comprehensions, or manually pass a tuple with the same module (but different ids), I get the same error:

12:03:17.095 [notice] Application permits_processor exited: PermitsProcessor.Application.start(:normal, []) returned an error: shutdown: failed to start child: :"ProducerConsumer-2"
    ** (EXIT) already started: #PID<0.195.0>

It works with single processes at each stage, but trying to spawn multiple just breaks it. I'm sure I'm overlooking something fundamental about processes, but after hours of little articles and code snippets out there I need someone to point me to the right direction. Thanks!

flat marsh
#

Found the issue. I had to pass a parameter in start_link/1 , an ID to give them different names in GenStage.start_link/3

upbeat cypress
flat marsh
upbeat cypress