Merge pull request #1122 from knowsuchagency/master

add app.register_listener method
This commit is contained in:
Raphael Deem
2018-02-15 16:58:27 -08:00
committed by GitHub
3 changed files with 44 additions and 0 deletions

View File

@@ -100,6 +100,20 @@ async def close_db(app, loop):
await app.db.close()
```
It's also possible to register a listener using the `register_listener` method.
This may be useful if you define your listeners in another module besides
the one you instantiate your app in.
```python
app = Sanic()
async def setup_db(app, loop):
app.db = await db_setup()
app.register_listener(setup_db, 'before_server_start')
```
If you want to schedule a background task to run after the loop has started,
Sanic provides the `add_task` method to easily do so.