django-event-system icon indicating copy to clipboard operation
django-event-system copied to clipboard

Why Listeners/Handlers get called on every request?

Open amitchavan90 opened this issue 2 years ago • 2 comments

I observed that whenever new request is initiated, django-event-system executes all listeners & handlers with empty data.

See below:

Following is my simple handler:

def receive_event(*args, **kwargs):
    print('event received')
    print(kwargs)
    print(kwargs.get('code'))
    print(kwargs.get('title'))

Dispatcher.RegisterHandler('snippets::events::SnippetsEvent', receive_event)

Following is my EventListener class:

class ExampleEventListener(EventListener):
    listensFor = [
        SnippetsEvent,
    ]

    def handle(self, event, *args, **kwargs):
        print("in ExampleEventListener handler")
        print(event)
        print(kwargs)
        pass
    pass

And I dispatch twice as follows (Seperate for both the listeners as we're struggling with #18):

SnippetsEvent.Dispatch(title=self.title, code=self.code)
Dispatcher.Dispatch('snippets::events::SnippetsEvent', code=self.code, title=self.title)

Now if you see the console log, it will show output like below: First POST request to our endpoint.

in ExampleEventListener handler
snippets::events::SnippetsEvent
{}
event received
{}
None
None
event dispatched
127.0.0.1 - - [2023-08-31 08:28:02] "POST /snippets/ HTTP/1.1" 201 526 0.218918
in ExampleEventListener handler
snippets::events::SnippetsEvent
{'code': 'print(1001)', 'title': 'foo-bar-123'}
event received
{'code': 'print(1001)', 'title': 'foo-bar-123'}
print(1001)
foo-bar-123

Again on second POST request it initializes/ calls listeners/handlers with empty data:

in ExampleEventListener handler
snippets::events::SnippetsEvent
{}
event received
{}
None
None
event dispatched
127.0.0.1 - - [2023-08-31 08:28:34] "POST /snippets/ HTTP/1.1" 201 526 0.065729
in ExampleEventListener handler
snippets::events::SnippetsEvent
{'code': 'print(1002)', 'title': 'foo-bar-124'}
event received
{'code': 'print(1002)', 'title': 'foo-bar-124'}
print(1002)
foo-bar-124

Biggest concerns is it calls in main request-response loop. And, it has added some extra latency in our response time.

amitchavan90 avatar Aug 31 '23 08:08 amitchavan90

I am not sure what is causing this, sounds like a bug. Do you have an example of your handler that you can share with me?

radding avatar Dec 27 '23 01:12 radding

@classmethod
    def HandleDispatch(cls):
        while True:
            lastDispatch = cls.queue.get()
            if cls.isTesting and lastDispatch["event"] in cls.killEvent:
                cls.called.add(lastDispatch["event"]) 
                continue
            for key, value in cls.handlers.items():
                if key.fullmatch(lastDispatch["event"]) is not None:
                    for i in value:
                        try:
                            # TODO: Investigate why it was 
                            # calling both Listener and Handler
                            if str(type(i)).find("Listener") < 0:
                                i(*lastDispatch["args"], **lastDispatch["kwargs"])
                        except Exception:
                            traceback.print_exc()

Can you check this line with the TODO comment. I think it is causing the listeners and the handlers to be called on every request. I tried adding this if str(type(i)).find("Listener") < 0: to prevent the listeners from being called on every request. You input would be appreciated. @radding

HeyMrQuaidoo avatar Jul 31 '24 10:07 HeyMrQuaidoo