Aguxez

Aguxez

A little confused about spawning new processes

I’m building a bot for a server just for fun and get a test of elixir more ‘in-depth’, the bot has to save state so I’m using Agents, everything is kinda working as intended for now. I’d like that the bot “creates” a new game room if it is in another server, so, it should spawn another process and save the new state to that pid, right? That’s what I understood about processes until now, how can I spawn a new process if a given command is executed? For example

>>start (Which is a command from the server to trigger the start_link() function)

Bot spawns a new process with an empty state but if I execute the >>start command again I get an error saying that the process is already running (I will save the id of the server so no more than one process is spawned for server), obviously not spawning a new process, so that’s my doubt, how can I spawn new processes with an empty state? Is that the way it should be done? Every process spawned has it’s own state and do not interfere with another one?

I’m using the module name as the name for the process, maybe that’s why the >>start command is always referenced to the same process? Sorry if I was not too clear with my issue, please tell me and I’ll elaborate.

Marked As Solved Switch mode

OvermindDL1

OvermindDL1

Eyup, don’t name your process, hold the pid instead. :slight_smile:

And yes, every process’s state/stack is distinct. :slight_smile:

Also Liked

net

net

Also, the result of the last expression in a case clause gets returned:

value =
  case true do
    true -> 1
    false -> 0
  end

value # 0
OvermindDL1

OvermindDL1

*Everything* returns something, and your @prefix <> "start" -> is returning {:ok, pid}, just assign the output of the case msg.content do to a variable and pass it around in whatever you are holding state in. ^.^

This would work for a single node, but not multi-node, so it depends on your use case.

OvermindDL1

OvermindDL1

Yeah you are not storing the pid anywhere here at all. ^.^

Assuming your state is a map, then you can do this:

def handle_event({:MESSAGE_CREATE, {msg}, _ws_state}, state) do
    case msg.content do
      @prefix <> "start" ->
        {:ok, pid} = Cass.Server.start_link(
          %{name: msg.author.username, id: msg.author.id}
        )
        %{state | pid: pid} # You return your updated state from this function, so you do that here
      @prefix <> "queue" ->
        Setup.get_agent(state.pid)
        state # Not changing the state so just passing it right back out
    end
end

So yeah, you were not actually storing the pid anywhere. ^.^

Last Post!

OvermindDL1

OvermindDL1

For note, you can initialize your state map in your init callback by setting it to, oh, %{pid: nil} or something.

Where Next?

Trending in Questions Top

jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
saveman71
Hello ! We want new/edit form pages to POST/PUT to their own URL rather than the resources REST defaults (post /things, put /things/:id)...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New

Other Trending Topics Top

jhogberg
Patch Package: OTP 29.0.5 Git Tag: OTP-29.0.5 Date: 2026-08-04 Trouble Report Id: OTP-...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
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
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New

We're in Beta

About us Mission Statement