I know I can call a GenServer like this
GenServer.call(pid, request)
# using a pid
or like this
GenServer.call(registered_name, request)
# if I registered the process with a name
But is there a way to excute the GenServer.call
inside the module without knowing the pid/registered name?(ie is there something like GenServer.call(__CURRENT_PROCESS__, request)
?)
GenServer behaviour (Elixir v1. 12.3) A behaviour module for implementing the server of a client-server relation. A GenServer is a process like any other Elixir process and it can be used to keep state, execute code asynchronously and so on.
What is OTP? OTP is an awesome set of tools and libraries that Elixir inherits from Erlang, a programming language on whose VM it runs. OTP contains a lot of stuff, such as the Erlang compiler, databases, test framework, profiler, debugging tools.
This will simply not work. A GenServer.call
is simply sending a message to the given process and then waiting for another message (the reply), blocking the current process. If you send a message to self
this way the process will not be free to handle that message as it will be blocked waiting for a reply. So the call
will always time out.
What I suspect you need is just to extract the functionality you want into a function and directly call that. Alternatively you can send a cast
to the current process, effectively telling it to do something "later".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With