Package: pymitter
Python port of the extended Node.js EventEmitter 2 approach ofhttps://github.com/asyncly/EventEmitter2providing namespaces, wildcards and TTL.Original source hosted atGitHub.FeaturesNamespaces with wildcardsTimes to listen (TTL)Usage via decorators or callbacksCoroutine supportLightweight implementation, good performanceInstallationSimply install viapip:pipinstallpymitterThe last version with Python 2 support wasv0.3.2(PyPI).ExamplesBasic usagefrompymitterimportEventEmitteree=EventEmitter()# decorator usage@ee.on("my_event")defhandler1(arg):print("handler1 called with",arg)# callback usagedefhandler2(arg):print("handler2 called with",arg)ee.on("my_other_event",handler2)# support for coroutine functions@ee.on("my_third_event")asyncdefhandler3(arg):print("handler3 called with",arg)# emitee.emit("my_event","foo")# -> "handler1 called with foo"ee.emit("my_other_event","bar")# -> "handler2 called with bar"ee.emit("my_third_event","baz")# -> "handler3 called with baz"CoroutinesWrappingasyncfunctions outside an event loop will start an internal event loop and calls toemitreturn synchronously.frompymitterimportEventEmitteree=EventEmitter()# register an async function@ee.on("my_event")asyncdefhandler1(arg):print("handler1 called with",arg)# emitee.emit("my_event","foo")# -> "handler1 called with foo"Wrappingasyncfunctions inside an event loop will use the same loop andemit_asyncis awaitable.frompymitterimportEventEmitteree=EventEmitter()asyncdefmain():# emit_asyncawaitable=ee.emit_async("my_event","foo")# -> nothing printed yetawaitawaitable# -> "handler1 called with foo"Useemit_futureto not return awaitable objects but to place them at the end of the existing event loop (usingasyncio.ensure_futureinternally).TTL (times to listen)frompymitterimportEventEmitteree=EventEmitter()@ee.once("my_event")defhandler1():print("handler1 called")@ee.on("my_event",ttl=2)defhandler2():print("handler2 called")ee.emit("my_event")# -> "handler1 called"# -> "handler2 called"ee.emit("my_event")# -> "handler2 called"ee.emit("my_event")# nothing called anymoreWildcardsfrompymitterimportEventEmitteree=EventEmitter(wildcard=True)@ee.on("my_event.foo")defhandler1():print("handler1 called")@ee.on("my_event.bar")defhandler2():print("handler2 called")@ee.on("my_event.*")defhander3():print("handler3 called")ee.emit("my_event.foo")# -> "handler1 called"# -> "handler3 called"ee.emit("my_event.bar")# -> "handler2 called"# -> "handler3 called"ee.emit("my_event.*")# -> "handler1 called"# -> "handler2 called"# -> "handler3 called"APIEventEmitter(*, wildcard=False, delimiter=".", new_listener=False, max_listeners=-1)EventEmitter constructor.Note: always usekwargsfor configuration.
WhenwildcardisTrue, wildcards are used as shown inthis example.delimiteris used to seperate namespaces within events.
Ifnew_listenerisTrue, the"new_listener"event is emitted every time a new listener is registered.
Functions listening to this event are passed(func, event=None).max_listenersdefines the maximum number of listeners per event.
Negative values mean infinity.on(event, func=None, ttl=-1)Registers a function to an event.
WhenfuncisNone, decorator usage is assumed.ttldefines the times to listen. Negative values mean infinity.
Returns the function.once(event, func=None)Registers a function to an event withttl = 1.
WhenfuncisNone, decorator usage is assumed.
Returns the function.on_any(func=None)Registers a function that is called every time an event is emitted.
WhenfuncisNone, decorator usage is assumed.
Returns the function.off(event, func=None)Removes a function that is registered to an event.
WhenfuncisNone, decorator usage is assumed.
Returns the function.off_any(func=None)Removes a function that was registered viaon_any().
WhenfuncisNone, decorator usage is assumed.
Returns the function.off_all()Removes all functions of all events.listeners(event)Returns all functions that are registered to an event.
Wildcards are not applied.listeners_any()Returns all functions that were registered usingon_any().listeners_all()Returns all registered functions.emit(event, *args, **kwargs)Emits an event.
All functions of events that matcheventare invoked withargsandkwargsin the exact order of their registeration.
Async functions are called in a new event loop.
There is no return value.(async) emit_async(event, *args, **kwargs)Emits an event.
All functions of events that matcheventare invoked withargsandkwargsin the exact order of their registeration.
Awaitable objects returned by async functions are awaited in the outer event loop.
Returns anAwaitable.emit_future(event, *args, **kwargs)Emits an event.
All functions of events that matcheventare invoked withargsandkwargsin the exact order of their registeration.
Awaitable objects returned by async functions are placed at the end of the event loop usingasyncio.ensure_future.
There is no return value.DevelopmentSource hosted atGitHubPython module hosted atPyPIReport issues, questions, feature requests onGitHub Issues