Process send_link doesn't work when the parent process dies, even after unlinking

huangapple go评论55阅读模式
英文:

Process send_link doesn't work when the parent process dies, even after unlinking

问题

I have:

Process.send_after(self(), {:do_thing, type, x, z, 0}, 60_000)

Then if the parent process dies that never happens
So I tried:

{:ok, pid} =
  Task.start_link(fn ->
    Process.send_after(self(), {:do_thing, type, x, z, 0}, 60_000)
  end)
Process.unlink(pid)

But that still didn't fix it. What do I do?

I also tried:

this = self() and then Process.send_after(this, {:do_thing, type, x, z, 0}, 60_000) and it didn't work.

英文:

I have

Process.send_after(self(), {:do_thing, type, x, z, 0}, 60_000)

Then if the parent process dies that never happens
So I tried

      {:ok, pid} =
        Task.start_link(fn ->
          Process.send_after(self(), {:do_thing, type, x, z, 0}, 60_000)
        end)
      Process.unlink(pid)

but that still didn't fix it. what do I do.

I also tried

this = self() and then Process.send_after(this, {:do_thing, type, x, z, 0}, 60_000) and it didn't work.

答案1

得分: 2

以下是您要翻译的内容:

根据 Process 模块的文档中所述:

如果给定的 dest 是一个不再存活或者给定的 PID 已经退出的进程,则定时器将被自动取消。

如果 dest 进程在定时器触发时仍然存活,那么可以制作一个示例。下面是一个示例,展示了一个 Dog 进程生成一个 Cat 进程,然后 Cat 进程调用 Process.send_after/3,向第三个进程发送消息。当定时器触发时,Dog 和 Cat 进程都不再存在。

defmodule Start do
  def spawn_dog do
    spawn(Dog, :bark, [self()])

    receive do
      msg -> IO.puts("Start process got message: #{msg}")
    end
  end
end

defmodule Dog do
  def bark(start_pid) do
    IO.puts("Dog process says bark.")
    spawn(Cat, :meow, [start_pid])

    IO.puts("Dog process exiting.")
  end
end

defmodule Cat do
  def meow(target_pid) do
    IO.puts("Cat process says meow.")
    Process.send_after(target_pid, "Cat wants food.", 3000) # Dog process above will exit
                                                            # before timer goes off.
    IO.puts("Cat process exiting.")  # Cat process will exit before timer goes off.
  end
end

在 iex 中:

iex(21)> c("start.ex")    
warning: redefining module Start (current version defined in memory)
  start.ex:1

warning: redefining module Dog (current version defined in memory)
  start.ex:12

warning: redefining module Cat (current version defined in memory)
  start.ex:22

[Cat, Dog, Start]

iex(22)> Start.spawn_dog()
Dog process says bark.
Dog process exiting.
Cat process says meow.
Cat process exiting.
Start process got message: Cat wants food.
:ok
英文:

As stated in the Process module docs:

> The timer will be automatically canceled if the given dest is a PID
> which is not alive or when the given PID exits.

An example can be made to work if the dest process is still alive when the timer goes off. Below is an example that shows a Dog process spawning a Cat process, then the Cat process calls Process.send_after/3, which sends a message back to a third process. Both the Dog and Cat process no longer exist when the timer goes off.

defmodule Start do
  def spawn_dog do
    spawn(Dog, :bark, [ self() ])

    receive do
      msg -> IO.puts("Start process got message: #{msg}")
    end

  end
end

defmodule Dog do
  def bark(start_pid) do
    IO.puts("Dog process says bark.")
    spawn(Cat, :meow, [start_pid])

    IO.puts("Dog process exiting.")
  end

end

defmodule Cat do
  def meow(target_pid) do
    IO.puts("Cat process says meow.")
    Process.send_after(target_pid, "Cat wants food.", 3000) #Dog process above will exit
                                                            #before timer goes off.
    IO.puts("Cat process exiting.")  #Cat process will exit before timer goes off.
  end
end

In iex:

iex(21)> c("start.ex")    
warning: redefining module Start (current version defined in memory)
  start.ex:1

warning: redefining module Dog (current version defined in memory)
  start.ex:12

warning: redefining module Cat (current version defined in memory)
  start.ex:22

[Cat, Dog, Start]

iex(22)> Start.spawn_dog()
Dog process says bark.
Dog process exiting.
Cat process says meow.
Cat process exiting.
Start process got message: Cat wants food.
:ok

huangapple
  • 本文由 发表于 2023年4月20日 05:24:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76058917.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定