Aguxez
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.
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
OvermindDL1
Eyup, don’t name your process, hold the pid instead.
And yes, every process’s state/stack is distinct.
Aguxez
Alright, that problem is solved then, after holding the pid in a variable I need to access it from other blocks in the same module, the commands are in a
case, since the>>startcommand starts the link with the Agent I can’t access the variable outside of that block, which can be the most efficient way of getting that pid? Thanks for the help.OvermindDL1
Well I certainly would not use an Agent, but to pass it out of a block you just return it from that block, what issue were you having with that?
Aguxez
Ah, sorry, not a block, it was a clause, as far as I know you can’t return values from
caseclauses, right?I have something like this:
Or what would you recommend me to do in this situation?
net
net
Also, the result of the last expression in a case clause gets returned:
OvermindDL1
*Everything* returns something, and your
@prefix <> "start" ->is returning{:ok, pid}, just assign the output of thecase msg.content doto 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.
Aguxez
Ah, I didn’t know that, thanks both for your input!
Aguxez
Argh, sorry but I think that I should explain my issue a little better and give more context, I’m using a library for Discord and the way that you listen for events is through a
handle_eventfunction and then you match the contents of the message with, well, what you’re expecting, this is part of my code:I’m getting errors saying that the pid variable doesn’t exist because of the scope, right? I mean, even if the process is running I can’t use that pid. I tried using the content of the variable as the return statement of the
case, something like:But, uh, I can’t use two
casestatements, so… I really don’t know how to initialize the process and pass the variable to get the state later onI need to explicitly initialize the process when the
>>startcommand is specified because then how can I spawn the process? I tried moving thestart_linkfunction above the case but that’d match every time a message is sent and that’s a total mess, if I just wanted to use the bot on a single server it would be done already. I thought I had it but I’m still confused… well, not confused because I understand the process, I just don’t know how to implement it…OvermindDL1
Yeah you are not storing the pid anywhere here at all. ^.^
Assuming your state is a map, then you can do this:
So yeah, you were not actually storing the pid anywhere. ^.^