是否可以在 Erlang 中构建工作队列?

发布于 2024-07-08 10:54:17 字数 125 浏览 6 评论 0原文

我在 Erlang 中见过很多聊天示例,但是列表(例如工作队列)又如何呢? 如果我想构建一个工作队列系统,例如项目管理系统,是否可以对流程邮箱中的消息重新排序,或者我是否必须使用消息优先级? 有Erlang构建的工作流系统的例子吗?

I have seen lots of chat examples in Erlang but what about lists, like a work queue? If I want to build a work queue system, like a project management system, is it possible to re-order messages in a process mailbox or do I have to use message priorities? Are there examples of workflow systems built in Erlang?

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

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

发布评论

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

评论(3

再见回来 2024-07-15 10:54:17

在 Erlang 中,您无法对进程消息队列中的消息进行重新排序。

但是,您可以进行选择性接收,这样您可以首先接收您认为最重要的消息。 它并不完全相同,但适用于大多数用途。

下面是一个示例:

receive
    {important, Msg} ->
        handle(Msg)
after 0 ->
    ok
end,
receive
    OtherMsg ->
        handle(Msg)
end

它与: 的不同之处

receive
    {important, Msg} ->
        handle(Msg);
    OtherMsg ->
        handle(Msg)
end

在于,在继续处理其余消息之前,它始终会扫描整个消息队列中的 {important, Msg}。 这意味着这些类型的消息将始终在其他消息(如果存在)之前得到处理。 这当然会带来一些性能成本(扫描整个队列两次需要更多时间)。

You cannot reorder messages in process message queues in Erlang.

You can, however do selective receives in which you can receive the message you deem most important first. It's not entirely the same but works for most purposes.

Here's an example:

receive
    {important, Msg} ->
        handle(Msg)
after 0 ->
    ok
end,
receive
    OtherMsg ->
        handle(Msg)
end

It differs from:

receive
    {important, Msg} ->
        handle(Msg);
    OtherMsg ->
        handle(Msg)
end

In that it will always scan the whole message queue for {important, Msg} before continuing handling the rest of the messages. It means that those kinds of messages will always be handled before any others, if they exist. This of course comes at some performance cost (it takes more time scanning the whole queue twice).

信仰 2024-07-15 10:54:17

进程邮箱对于作业队列来说工作得很好。

只需让您的邮件包含足够的信息,以便轻松编写选择性接收模式,并且您不会觉得需要重新排序邮箱内容。

Process mailboxes work quite well as-is for job queues.

Just have your messages include sufficient information so that selective receive patterns are easy to write, and you won't feel the need to re-order mailbox contents.

绿萝 2024-07-15 10:54:17

如果您确实需要重新排序邮件,则可以遵循网守模式:将邮箱具体化为单独的进程。 当您的原始进程准备好接收另一条消息时,网守可以根据您选择的任何规则计算要转发的消息。

If you do need to reorder messages, you can follow the gatekeeper pattern: reify the mailbox as a separate process. When your original process is ready for another message, the gatekeeper can compute which message to forward, by any rule you choose.

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