mediator icon indicating copy to clipboard operation
mediator copied to clipboard

Using Mediator in Multiple Files

Open mmarab opened this issue 7 years ago • 1 comments

Hi, this maybe a simple question, but i'm pretty new to python and Mediator. I'm currently using Mediator, but i will be dispatching events from different api controllers which live in different python files, i would like to use a singleton instance of Mediator so i'm not having to new up all the listeners etc with each request, how would you do this with your framework? I thought defining Mediator as a static from a setup class could work...

mmarab avatar Jan 22 '18 14:01 mmarab

Hi @mmarab! Sorry for delay.

I thought defining Mediator as a static from a setup class could work...

And what's the problem?

For example, you have in app.py:

mediator = Mediator()
# and then register your listeners...

view1.py:

from app import mediator

def view(req):
    mediator.dispatch(event1)

view2.py:

from app import mediator

def view(req):
    mediator.dispatch(event2)

This setup should work fine.

If you have a different files for listeners, you can use venusian's scanner or import them manually.

ghost avatar Feb 24 '18 14:02 ghost