Skip to content

Commit

Permalink
Add __rsgi_del__ support (#322) (#343)
Browse files Browse the repository at this point in the history
  • Loading branch information
gi0baro authored Jul 7, 2024
1 parent 689742c commit 6ae5bf5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
24 changes: 24 additions & 0 deletions docs/spec/RSGI.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,30 @@ class App:
# RSGI protocol handling
```

#### `__rsgi_del__` method

The del method provides a way for RSGI applications to perform cleanup operations during server shutdown.

The signature of `__rsgi_del__` is defined as follows, with the `loop` argument being the Python `asyncio` event loop:

```
function __rsgi_del__(loop)
```

> **Note:** the event loop won't be running at the time the del function gets called
Thus, an application exposing the RSGI del interface might look like the following:

```python
class App:
def __rsgi_del__(self, loop):
some_sync_cleanup_task()
loop.run_until_complete(some_async_cleanup_task())

async def __rsgi__(self, scope, protocol):
# RSGI protocol handling
```

## Protocols

### HTTP protocol
Expand Down
4 changes: 4 additions & 0 deletions granian/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,9 @@ def _spawn_rsgi_worker(
callback_init = (
getattr(target, '__rsgi_init__') if hasattr(target, '__rsgi_init__') else lambda *args, **kwargs: None
)
callback_del = (
getattr(target, '__rsgi_del__') if hasattr(target, '__rsgi_del__') else lambda *args, **kwargs: None
)
callback = _rsgi_call_wrap(callback, log_access_fmt)

shutdown_event = set_loop_signals(loop, [signal.SIGTERM, signal.SIGINT])
Expand All @@ -335,6 +338,7 @@ def _spawn_rsgi_worker(
contextvars.copy_context(),
shutdown_event,
)
callback_del(loop)

@staticmethod
def _spawn_wsgi_worker(
Expand Down

0 comments on commit 6ae5bf5

Please sign in to comment.