|
1 | 1 | import functools |
2 | 2 |
|
3 | | -from asgiref.sync import async_to_sync |
| 3 | +from asgiref.sync import async_to_sync, sync_to_async |
4 | 4 |
|
5 | 5 | from . import DEFAULT_CHANNEL_LAYER |
6 | 6 | from .db import database_sync_to_async |
|
10 | 10 | from .utils import await_many_dispatch |
11 | 11 |
|
12 | 12 |
|
| 13 | +async def _asend_wrapper(signal, **kwargs): |
| 14 | + """ |
| 15 | + Signal.asend was introduced in Django 5.0, thus a wrapper is needed for older versions. |
| 16 | + """ |
| 17 | + if hasattr(signal, "asend"): |
| 18 | + await signal.asend(**kwargs) |
| 19 | + else: |
| 20 | + await sync_to_async(signal.send)(**kwargs) |
| 21 | + |
| 22 | + |
13 | 23 | def get_handler_name(message): |
14 | 24 | """ |
15 | 25 | Looks at a message, checks it has a sensible type, and returns the |
@@ -63,15 +73,15 @@ async def __call__(self, scope, receive, send): |
63 | 73 | await await_many_dispatch([receive], self.dispatch) |
64 | 74 | except StopConsumer: |
65 | 75 | # Exit cleanly |
66 | | - await consumer_terminated.asend(sender=self.__class__) |
| 76 | + await _asend_wrapper(consumer_terminated, sender=self.__class__) |
67 | 77 |
|
68 | 78 | async def dispatch(self, message): |
69 | 79 | """ |
70 | 80 | Works out what to do with a message. |
71 | 81 | """ |
72 | 82 | handler = getattr(self, get_handler_name(message), None) |
73 | 83 | if handler: |
74 | | - await consumer_started.asend(sender=self.__class__) |
| 84 | + await _asend_wrapper(consumer_started, sender=self.__class__) |
75 | 85 | await handler(message) |
76 | 86 | else: |
77 | 87 | raise ValueError("No handler for message type %s" % message["type"]) |
|
0 commit comments