对 wf:insert_bottom 的两次快速连续调用以相反的顺序结束

发布于 2024-08-16 09:29:23 字数 1103 浏览 5 评论 0原文

使用 Nitrogen,Erlang Web 框架,我有以下方法来接收消息并将它们添加到 html 的底部element:

receive_messages() ->
  receive
    Message ->
      io:format("~p received ~p", [self(), Message]),
      wf:insert_bottom(messages, [#p{}, #span { text=io_lib:format("~p", [Message]) }])
  end,
  wf:comet_flush(),
  receive_messages().

它被设置为 comet 的通常方式:

wf:comet(fun() -> receive_messages() end)

它很快收到两条消息:

<0.907.0> received {starting_chat,<0.905.0>}
<0.907.0> received {connected_to,<0.902.0>}

这是我在 HTML 中看到的:

{connected_to, <0.902.0>}
{starting_chat, <0.905.0>}

不知何故,它们以相反的顺序结束。

我已经开始向此方法添加timer:sleep() 调用。 50 毫秒时,它们的顺序正确,20 毫秒时则不正确。当它们的顺序不正确时,它们似乎总是处于错误的顺序,这看起来非常确定。

有什么想法吗?这是一个错误吗?除了睡觉之外,我应该做什么才能让它们按正确的顺序排列?

另外在邮件列表中询问,以防那里有更多信息。

Using Nitrogen, the Erlang web framework, I have the following method that receives messages and adds them to the bottom of an html element:

receive_messages() ->
  receive
    Message ->
      io:format("~p received ~p", [self(), Message]),
      wf:insert_bottom(messages, [#p{}, #span { text=io_lib:format("~p", [Message]) }])
  end,
  wf:comet_flush(),
  receive_messages().

It is set as the usual way for comet:

wf:comet(fun() -> receive_messages() end)

It receives two messages very quickly:

<0.907.0> received {starting_chat,<0.905.0>}
<0.907.0> received {connected_to,<0.902.0>}

This is what I see in the HTML:

{connected_to, <0.902.0>}
{starting_chat, <0.905.0>}

Somehow, they ended in the reverse order.

I've started adding timer:sleep() calls to this method. With 50 milliseconds, they are in the correct order, with 20 they are not. When they are in the incorrect order they seem to be always in the incorrect order, it seems very deterministic.

Any ideas why? Is this a bug? Any ideas what should I do to get them in the correct order other than sleeping?

Also asked on the mailing list, in case there's more info there.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无人接听 2024-08-23 09:29:23

wf_comet.erl 有一个“奇怪的”反向调用应用inner_collect_content后。我的猜测是,inner_collect_content 以前曾经是尾递归的。

如果您应用超时,您的消息将在不同的循环中一一收集,因此顺序不再颠倒。


另一个存储库上似乎有一个提交修复了这个问题:

http://github.com/gersh/氮/提交/a8bfcb23d003e68f7394a0455285beeb0fbf9b09

wf_comet.erl has a "strange" reverse call after applying inner_collect_content. My guess is that inner_collect_content used to be tail-recursive sometime ago.

If you apply the timeout, your messages are collected in different loops, one-by-one, so the order is not reversed anymore.


There seems to be a commit fixing this on another repository:

http://github.com/gersh/nitrogen/commit/a8bfcb23d003e68f7394a0455285beeb0fbf9b09

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文