Lahja is a generic multi process event bus implementation written in Python 3.6+ to enable lightweight inter-process communication, based on non-blocking asyncio
MIT License
394
stars
19
forks
source link
Context manager API for EndpointAPI.subscribe #149
The EndpointAPI.subscribe API returns a handle on the subscription that the user can call to remove this subscription. This is easy to forget or in some contexts, not always easy have a clear place where this would be called.
How can it be fixed?
We can fix this by making lahja.common.Subscription a context manager and having it automatically call Subscription.unsubscribe in the __exit__. This allows for the subscription API to be used like this:
with EndpointAPI.subscribe(MyEvent, handler_fn):
# subscription active here:
# automatically unsubscribed here
What was wrong?
The
EndpointAPI.subscribe
API returns a handle on the subscription that the user can call to remove this subscription. This is easy to forget or in some contexts, not always easy have a clear place where this would be called.How can it be fixed?
We can fix this by making
lahja.common.Subscription
a context manager and having it automatically callSubscription.unsubscribe
in the__exit__
. This allows for the subscription API to be used like this: