Adapt a method to be in the correct "mode": - If is_async is False: - Synchronous methods are left alone - Asynchronous methods are wrapped with async_to_sync - If is_async is True: - Synchronous methods are wrapped with sync_to_async()
(
self,
is_async,
method,
method_is_async=None,
debug=False,
name=None,
)
| 104 | self._middleware_chain = handler |
| 105 | |
| 106 | def adapt_method_mode( |
| 107 | self, |
| 108 | is_async, |
| 109 | method, |
| 110 | method_is_async=None, |
| 111 | debug=False, |
| 112 | name=None, |
| 113 | ): |
| 114 | """ |
| 115 | Adapt a method to be in the correct "mode": |
| 116 | - If is_async is False: |
| 117 | - Synchronous methods are left alone |
| 118 | - Asynchronous methods are wrapped with async_to_sync |
| 119 | - If is_async is True: |
| 120 | - Synchronous methods are wrapped with sync_to_async() |
| 121 | - Asynchronous methods are left alone |
| 122 | """ |
| 123 | if method_is_async is None: |
| 124 | method_is_async = iscoroutinefunction(method) |
| 125 | if debug and not name: |
| 126 | name = name or "method %s()" % method.__qualname__ |
| 127 | if is_async: |
| 128 | if not method_is_async: |
| 129 | if debug: |
| 130 | logger.debug("Synchronous handler adapted for %s.", name) |
| 131 | return sync_to_async(method, thread_sensitive=True) |
| 132 | elif method_is_async: |
| 133 | if debug: |
| 134 | logger.debug("Asynchronous handler adapted for %s.", name) |
| 135 | return async_to_sync(method) |
| 136 | return method |
| 137 | |
| 138 | def get_response(self, request): |
| 139 | """Return an HttpResponse object for the given HttpRequest.""" |