mirror of
https://github.com/Brandon-Rozek/website.git
synced 2024-11-22 08:16:29 -05:00
New Post
This commit is contained in:
parent
f8d002207d
commit
37219ffbb6
1 changed files with 23 additions and 0 deletions
23
content/blog/asynccallbacks.md
Normal file
23
content/blog/asynccallbacks.md
Normal file
|
@ -0,0 +1,23 @@
|
|||
---
|
||||
title: "Quick Python: Async Callbacks"
|
||||
date: 2020-07-11T20:23:29-04:00
|
||||
draft: false
|
||||
tags: ["python"]
|
||||
---
|
||||
|
||||
I've written a post on using [callbacks in Python](https://brandonrozek.com/blog/pysubscribepattern/). Though to add callbacks to `asyncio` functions, you'll have to interact with the loop object directly. Replace the emit function in the previous post with the following:
|
||||
```python
|
||||
class Application:
|
||||
# ...
|
||||
def emit(self, message):
|
||||
for callback in self.callbacks:
|
||||
if asyncio.iscoroutine(callback):
|
||||
loop = asyncio.get_running_loop()
|
||||
loop.call_soon(
|
||||
functools.partial(
|
||||
asyncio.ensure_future,
|
||||
callback(event, index)
|
||||
)
|
||||
else:
|
||||
callback(event, index)
|
||||
```
|
Loading…
Reference in a new issue