Trying to make a short example with django 5 async signal. Here is the code:
View:
async def confirm_email_async(request, code):
await user_registered_async.asend(
sender=User,
)
return JsonResponse({"status": "ok"})
Signal:
user_registered_async = Signal()
@receiver(user_registered_async)
async def async_send_welcome_email(sender, **kwargs):
print("Sending welcome email...")
await asyncio.sleep(5)
print("Email sent")
The error trace is:
Traceback (most recent call last):
File "C:\Users\karonator\AppData\Roaming\Python\Python311\site-packages\asgiref\sync.py", line 534, in thread_handler
raise exc_info[1]
File "C:\Users\karonator\AppData\Roaming\Python\Python311\site-packages\django\core\handlers\exception.py", line 42, in inner
response = await get_response(request)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\karonator\AppData\Roaming\Python\Python311\site-packages\asgiref\sync.py", line 534, in thread_handler
raise exc_info[1]
File "C:\Users\karonator\AppData\Roaming\Python\Python311\site-packages\django\core\handlers\base.py", line 253, in _get_response_async
response = await wrapped_callback(
^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\karonator\Desktop\signals\main\views.py", line 34, in confirm_email_async
await user_registered_async.asend(
File "C:\Users\karonator\AppData\Roaming\Python\Python311\site-packages\django\dispatch\dispatcher.py", line 250, in asend
responses, async_responses = await asyncio.gather(
^^^^^^^^^^^^^^^
File "C:\Program Files\Python311\Lib\asyncio\tasks.py", line 819, in gather
if arg not in arg_to_fut:
^^^^^^^^^^^^^^^^^^^^^
TypeError: unhashable type: 'list'
Will be grateful for any help, already broken my head. Thanks for your time.
In my opinion this is a bug in Django 5. I came across the same problem and created a report: https://code.djangoproject.com/ticket/35174. The
asendmethod (as well asasend_robust) crashes if the signal does not have at least one synchronous receiver.Here is a complete example of how you can use async in your project before the official fix is available (if it is indeed a bug):
Update 1
Bugreport in Django project has been accepted, I prepared a PR: https://github.com/django/django/pull/17837.
Update 2
Fix should be released in Django 5.0.3 (https://docs.djangoproject.com/en/5.0/releases/5.0.3/).