Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Same method name for multiple subscribers bug #3

Open
ddanny opened this issue Mar 5, 2019 · 0 comments
Open

Same method name for multiple subscribers bug #3

ddanny opened this issue Mar 5, 2019 · 0 comments
Labels
bug Something isn't working

Comments

@ddanny
Copy link

ddanny commented Mar 5, 2019

Please see the code below. To summarize:

  • Define one event
  • Define two subscriber listening for the event above. Each subscriber has a listener method with the name on_event
  • Each of the subscriber classes above defines an instance field, but with unique name (self.something in the first class, self.something2 in the second class)
  • Define another class that posts an event

Run this scenario and get the error below:

Exception in thread thread-on_event:
Traceback (most recent call last):
  File "C:\Anaconda2\envs\python\lib\threading.py", line 801, in __bootstrap_inner
    self.run()
  File "C:\Anaconda2\envs\python\lib\site-packages\pyeventbus\pyeventbus.py", line 112, in run
    self.method(self.subscriber, self.event)
  File "C:/FractureID/projects/python/ui/spectraqc/PyEventBusBug.py", line 16, in on_event
    print (self.something)
AttributeError: Subscriber2 instance has no attribute 'something'

Exception in thread thread-on_event:
Traceback (most recent call last):
  File "C:\Anaconda2\envs\python\lib\threading.py", line 801, in __bootstrap_inner
    self.run()
  File "C:\Anaconda2\envs\python\lib\site-packages\pyeventbus\pyeventbus.py", line 112, in run
    self.method(self.subscriber, self.event)
  File "C:/FractureID/projects/python/ui/spectraqc/PyEventBusBug.py", line 26, in on_event
    print (self.something_else)
AttributeError: Subscriber1 instance has no attribute 'something_else'

It complains about the variable in class two not having the attribute in the first class and the other way around.

If I change on of the on_event to something else like on_event2 then the issue is gone.

from pyeventbus import *


class SomeEvent:
    def __init__(self):
        pass


class Subscriber1:
    def __init__(self):
        self.something = 'First subscriber'
        PyBus.Instance().register(self, self.__class__.__name__)

    @subscribe(threadMode=Mode.PARALLEL, onEvent=SomeEvent)
    def on_event(self, event):
        print (self.something)


class Subscriber2:
    def __init__(self):
        self.something_else = 'Second subscriber'
        PyBus.Instance().register(self, self.__class__.__name__)

    @subscribe(threadMode=Mode.PARALLEL, onEvent=SomeEvent)
    def on_event(self, event):
        print (self.something_else)


class PyEventBusBug:

    def __init__(self):
        Subscriber1()
        Subscriber2()
        PyBus.Instance().post(SomeEvent())


if __name__ == "__main__":
    PyEventBusBug()

@n89nanda n89nanda added the bug Something isn't working label Mar 6, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants