I am using GenStage
and starting a few processes through it.
The code bit which is starting the process:
defp start_snapshot_extractor(config, id) do
config = Map.put(config, :id, id)
case Process.whereis(:snapshot_extractor) do
nil ->
{:ok, pid} = GenStage.start_link(EvercamMedia.SnapshotExtractor.CloudExtractor, {}, name: :snapshot_extractor)
pid
pid -> pid
end
|> GenStage.cast({:snapshot_extractor, config})
end
and in this module EvercamMedia.SnapshotExtractor.CloudExtractor
defmodule EvercamMedia.SnapshotExtractor.CloudExtractor do
use GenStage
require Logger
import Commons
import EvercamMedia.Snapshot.Storage
@root_dir Application.get_env(:evercam_media, :storage_dir)
def init(args) do
{:producer, args}
end
def handle_cast({:snapshot_extractor, config}, state) do
IO.inspect "here "
_start_extractor(config)
{:noreply, [], state}
end
end
Now the issue is. I am using an endpoint to start this process with different configurations, sometimes with the same configuration.
When I first start the process it prints "here "
and then after the completion of this process. it outputs "here "
again, why is that the case? Instead of running both processes parallel why it's waiting for the first one to complete and then run?
Update:
this is how first method is being called
extraction_pid = spawn(fn ->
EvercamMedia.UserMailer.snapshot_extraction_started(full_snapshot_extractor, "Cloud")
start_snapshot_extractor(config)
end)
:ets.insert(:extractions, {exid <> "-cloud-#{full_snapshot_extractor.id}", extraction_pid})
GenStage
in the first place? The issue here is that theGenServer
behind your producer is receiving messages from the process mailbox only upon the completion of previoushandle_cast/2. You need to start multiple instances, probably managed by
DynamicSupervisor`. – Aleksei Matiushkinname: :snapshot_extractor
from the call toGenStage.start_link/3
only anonymous processes can be started that way; the named one would return:already_started
. – Aleksei Matiushkinsnapshot_extractor
it tosnapshot_extractor_anynumber
? – Junaid Farooq