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
OvermindDL1
Eyup, don’t name your process, hold the pid instead. ![]()
And yes, every process’s state/stack is distinct. ![]()
Also Liked
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
*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
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. ^.^
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








