英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论