How to use an event driven architecture in Julia?

An event is a computation of something that has been composed earlier. For example if you have a macro:

macro event(expr)
    esc(:(()->($expr)))
end

it gives you back a closure that has captured your expression expr: any function and arguments wrapped in it, which you can put in a queue and execute when it is due, for example:

julia> myqueue = [];

julia> ev = @event begin    # compose an event
           # do something
           1+1
       end
#13 (generic function with 1 method)

julia> push!(myqueue, ev);  # push it around

Then later (or somewhere else in your program) you can do

julia> pop!(myqueue)()      # execute it later
2

And there maybe other events in your system composed after that one but with higher priority or different time stamp … that you or your scheduler decides to execute before.

There are many ways to do that, since functions in Julia are first class objects which you can pass around with or without arguments as

You can send your events over a Channel to another Task, even residing on another thread, worker or node. But be aware, that if you do that, you may encounter a synchronization problem, since those foreign threads or workers have another time and don’t know about the function you are referring to in your event (a world age problem).

Julia gives you all those possibilities and it is an exciting field to explore.

3 Likes