Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to build work queues in 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?

like image 596
John Wright Avatar asked Nov 03 '08 04:11

John Wright


2 Answers

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).

like image 71
2 revs Avatar answered Sep 28 '22 14:09

2 revs


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.

like image 35
Justin Sheehy Avatar answered Sep 28 '22 14:09

Justin Sheehy