# How to use an event driven architecture in Julia?

**URL:** <https://discourse.julialang.org/t/how-to-use-an-event-driven-architecture-in-julia/56748>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [March 8, 2021, 5:00pm UTC](https://discourse.julialang.org/t/how-to-use-an-event-driven-architecture-in-julia/56748 "2021-03-08T17:00:55Z")\
**Posts on this page:** 1\
**Showing post:** 10

<div class="post-metadata">

**Author:** ![pbayer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pbayer/32/11675_2.png) [@pbayer](https://discourse.julialang.org/u/pbayer)\
**Post date:** [March 9, 2021, 8:01am UTC](https://discourse.julialang.org/t/how-to-use-an-event-driven-architecture-in-julia/56748/10 "2021-03-09T08:01:00Z")

</div>

> [@BryanB](#):
>
> learning about event driven architecture and I’d like to know if it’s possible to use this approach in Julia?

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

```julia
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
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
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

- [closures](https://docs.julialang.org/en/v1.6-dev/devdocs/functions/#Closures) as mentioned,
- [function objects](https://en.wikipedia.org/wiki/Function_object#In_Julia) (functors),
- a `struct` containing a [function, its arguments and keyword arguments](https://pbayer.github.io/DiscreteEvents.jl/stable/events/#DiscreteEvents.fun)
- or a combination of the above.

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](https://discourse.julialang.org/t/world-age-problem-explanation/9714)).

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

---

_[View the full topic](https://discourse.julialang.org/t/how-to-use-an-event-driven-architecture-in-julia/56748)._
