website/content/blog/pysubscribepattern.md

148 lines
4 KiB
Markdown
Raw Permalink Normal View History

2020-04-14 08:42:49 -04:00
---
title: "Python Patterns: Subscribe"
date: 2020-04-14T07:53:46-04:00
draft: false
2022-01-02 14:24:29 -05:00
tags: ["Python"]
2023-01-05 14:04:45 -05:00
medium_enabled: true
2020-04-14 08:42:49 -04:00
---
It is common for larger applications to have modules that publishes and subscribes to events. This post will outline a couple ways to achieve this using [decorators](/blog/pydecorators/) and standard methods.
2020-04-14 08:42:49 -04:00
## Single Event
First let us concern ourselves with a single event since that's the easiest. Here we will create an application class that stores callbacks of functions through the subscribe decorator. Calling `emit` will send a message to all the functions stored in `self.callbacks`.
```python
2020-07-11 20:18:46 -04:00
from typing import Callable, List
2020-04-14 08:42:49 -04:00
class Application:
def __init__(self):
2020-07-11 20:18:46 -04:00
self.callbacks: List[Callable] = []
def subscribe(self, func: Callable):
2020-05-21 11:03:01 -04:00
if not callable(func):
raise ValueError("Argument func must be callable.")
2020-04-14 08:42:49 -04:00
self.callbacks.append(func)
return func
def emit(self, message):
for callback in self.callbacks:
callback(message)
```
Here is an example of its usage:
```python
app = Application()
@app.subscribe
def test1(message):
print("Function 1:", message)
def test2(message):
print("Function 2:", message)
app.subscribe(test2)
2020-04-14 08:42:49 -04:00
app.emit('Hello World')
```
```
Function 1: Hello World
Function 2: Hello World
```
## Multiple Events
Let's say you want the application to handle different types of events. Now `self.callbacks` is a dictionary of lists, where the key is the event and the list is the same as the last section. There's an additional layered function on top of `subscribe` this time in order to handle passing an argument into the decorator.
```python
from collections import defaultdict
2020-07-11 20:18:46 -04:00
from typing import Callable, Optional
2020-04-14 08:42:49 -04:00
class Application:
def __init__(self):
2020-07-11 20:18:46 -04:00
self.callbacks: Dict[str, List[Callable]] = defaultdict(list)
def on(self, event: str, func: Optional[Callable] = None):
def subscribe(func: Callable):
2020-05-21 11:03:01 -04:00
if not callable(func):
raise ValueError("Argument func must be callable.")
2020-04-14 08:42:49 -04:00
self.callbacks[event].append(func)
return func
2020-05-21 10:58:08 -04:00
if func is None:
return subscribe
2020-05-21 10:58:08 -04:00
subscribe(func)
2020-04-14 08:42:49 -04:00
def emit(self, event, message):
for callback in self.callbacks[event]:
callback(message)
```
To show its usage lets first create an instance of `Application`
```python
app = Application()
```
Now let's subscribe a couple functions to `event1`
```python
@app.on('event1')
def test1(message):
print("Function 1:", message)
def test3(message):
print("Function 3:", message)
app.on('event1', test3)
2020-04-14 08:42:49 -04:00
```
Now to subscribe a couple events to `event2`
```python
# Subscribed to event 2
@app.on('event2')
def test2(message):
print("Function 2:", message)
def test4(message):
print("Function 4:", message)
app.on('event2', test4)
2020-04-14 08:42:49 -04:00
```
We can also subscribe to both events
```python
# Subscribed to both events
@app.on('event1')
@app.on('event2')
def test5(message):
print("Function 5:", message)
```
```python
app.emit('event1', 'Hello, World!')
```
```
Function 1: Hello, World!
Function 3: Hello, World!
Function 5: Hello, World!
```
```python
app.emit('event2', 'Goodbye, World!')
```
```
Function 2: Goodbye, World!
Function 4: Goodbye, World!
Function 5: Goodbye, World!
```
2024-07-28 03:38:20 -04:00
## Alternative: Observer Pattern
The above approaches assume that the callbacks would be needed for the lifetime of the program. An altnerative is to use the [Observer design pattern](https://en.wikipedia.org/wiki/Observer_pattern).
This assumes that we have two classes, `Observable` and `Observer`. The latter class implements the `notify` method and when it is constructed, it adds itself to the collection contained in the `Observable` class.
[Martin](https://martinheinz.dev/blog/112) wrote a great blog post on how you can use Python weak references so that when your `Observer` class goes out of scope, a copy isn't stored in the `Observable` class and instead gets safely removed from the collection.