我不知道如何匹配异步函数和handle_info
此代码有效:
def order_month() do
Task.async(fn ->
(1..12)
|> Enum.map(fn a -> %{months: a} |> Map.put(:order_months, Proyect.BuyHandler.order_month(a |> Integer.to_string())
|> Enum.map(fn m -> m |> Proyect.BuyHandler.get_opc() end))end)
end)
end
我的目的是通过这种方式接收它:
def handle_info({_ref, %{months: month, order_months: order_months}}, socket) do
{:noreply, assign(socket, %{months: month, order_months: order_months} )}
Task.async/1
旨在产生的结果Task.await/2
。
无论是否要使用接收结果handle_info/2
,都应将生成的(例如with Kernel.spawn/1
)过程的结果显式发送到父过程。
您没有显示的代码Proyect.BuyHandler.get_opc/1
,但是如果我们假设它进行了简单的转换,我们可能会从那里发送消息(在这种情况下,Task.start/1
应使用而不是Task.async/1
在不链接的情况下启动该过程。)可以使用类似的方法。
def order_month(pid) do
Task.start(fn ->
(1..12)
|> Enum.map(fn a ->
%{months: a,
order_months: Proyect.BuyHandler.order_month("#{a}")}
end)
|> Enum.map(&Proyect.BuyHandler.get_opc/1)
# ⇓⇓⇓⇓⇓ THIS ⇓⇓⇓⇓⇓
|> Enum.each(&send(pid, &1))
end)
end
handle_info/2
def handle_info(%{}}, socket)
在这种情况下,它本身可能应该具有签名。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句