archimondain

archimondain

Killing a process synchronously

I would like to know if the function

Process.exit(pid, :kill)

Is synchronous : once the function has returned, are we sure that no more code from the process pid will be executed ? If not how could we kill a process in a synchronous way ?

Best

Marked As Solved

lud

lud

I am not sure that the function is synchronous, but to be sure, you could first set a monitor on the process with Process.monitor, then kill it, then wait for the :DOWN message from the monitor.

Also Liked

Qqwy

Qqwy

TypeCheck Core Team

Because the Elixir documentation (as well as the Erlang documentation of :erlang.exit/2, which this function delegates to) only mention “sends an exit signal”, I am fairly certain that the function is asynchronous.

The approach mentioned by @lud works. However, this will not (always) prevent other code from running on pid before it exits. It will only prevent code running on the process calling Process.exit to wait with continuing before pid has exited.

However, another question would be: what are you trying to do? It seems like a rather odd request. Maybe your actual problem can be resolved in a wholly different manner.

Qqwy

Qqwy

TypeCheck Core Team

From the documentation of Process.monitor/1:

If the process is already dead when calling Process.monitor/1 , a :DOWN message is delivered immediately.

So you will receive the :DOWN message in that case as well.

lud

lud

Also if you call Process.monitor on a pid that is not alive, the down message will contain :noproc as the reason, so you will know your process was not alive.

zkessin

zkessin

if you use spawn_monitor/3 you will remove the race condition on starting the process

Where Next?

Popular in Questions Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

Other popular topics Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New

We're in Beta

About us Mission Statement