Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I execute a call to the current process in a GenServer?

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)?)

like image 633
user3790827 Avatar asked Feb 12 '16 14:02

user3790827


People also ask

What is Elixir GenServer?

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 Elixir?

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.


1 Answers

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".

like image 118
Paweł Obrok Avatar answered Nov 03 '22 00:11

Paweł Obrok