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.

Last Post!

zkessin

zkessin

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

Where Next?

Popular in Questions Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
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
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New

Other popular topics Top

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
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New

We're in Beta

About us Mission Statement