Home:ALL Converter>Error in AsyncConsumer in Django channels

Error in AsyncConsumer in Django channels

Ask Time:2020-05-28T00:39:25         Author:Nimda

Json Formatter

I used a tutorial for creating a video chat app. But when i run the code it gives me error and it's not so clear to know what is exactly making the problem.

Error:

WebSocket CONNECT /ws/videochat/ [127.0.0.1:38466]
Exception inside application: You cannot call this from an async context - use a thread or sync_to_async.
Traceback (most recent call last):
  File "/home/nimda/Desktop/videotest/test/lib/python3.6/site-packages/django/db/models/fields/related_descriptors.py", line 172, in __get__
    rel_obj = self.field.get_cached_value(instance)
  File "/home/nimda/Desktop/videotest/test/lib/python3.6/site-packages/django/db/models/fields/mixins.py", line 13, in get_cached_value
    return instance._state.fields_cache[cache_name]
KeyError: 'callee'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/nimda/Desktop/videotest/test/lib/python3.6/site-packages/channels/sessions.py", line 183, in __call__
    return await self.inner(receive, self.send)
  File "/home/nimda/Desktop/videotest/test/lib/python3.6/site-packages/channels/sessions.py", line 183, in __call__
    return await self.inner(receive, self.send)
  File "/home/nimda/Desktop/videotest/test/lib/python3.6/site-packages/channels/middleware.py", line 41, in coroutine_call
    await inner_instance(receive, send)
  File "/home/nimda/Desktop/videotest/test/lib/python3.6/site-packages/channels/consumer.py", line 59, in __call__
    [receive, self.channel_receive], self.dispatch
  File "/home/nimda/Desktop/videotest/test/lib/python3.6/site-packages/channels/utils.py", line 51, in await_many_dispatch
    await dispatch(result)
  File "/home/nimda/Desktop/videotest/test/lib/python3.6/site-packages/channels/consumer.py", line 73, in dispatch
    await handler(message)
  File "/home/nimda/Desktop/videotest/vcp/chat/consumers.py", line 156, in websocket_receive
    f"videochat_{videothread.callee.id}",
  File "/home/nimda/Desktop/videotest/test/lib/python3.6/site-packages/django/db/models/fields/related_descriptors.py", line 186, in __get__
    rel_obj = self.get_object(instance)
  File "/home/nimda/Desktop/videotest/test/lib/python3.6/site-packages/django/db/models/fields/related_descriptors.py", line 153, in get_object
    return qs.get(self.field.get_reverse_related_filter(instance))
  File "/home/nimda/Desktop/videotest/test/lib/python3.6/site-packages/django/db/models/query.py", line 411, in get
    num = len(clone)
  File "/home/nimda/Desktop/videotest/test/lib/python3.6/site-packages/django/db/models/query.py", line 258, in __len__
    self._fetch_all()
  File "/home/nimda/Desktop/videotest/test/lib/python3.6/site-packages/django/db/models/query.py", line 1261, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "/home/nimda/Desktop/videotest/test/lib/python3.6/site-packages/django/db/models/query.py", line 57, in __iter__
    results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
  File "/home/nimda/Desktop/videotest/test/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1149, in execute_sql
    cursor = self.connection.cursor()
  File "/home/nimda/Desktop/videotest/test/lib/python3.6/site-packages/django/utils/asyncio.py", line 24, in inner
    raise SynchronousOnlyOperation(message)
django.core.exceptions.SynchronousOnlyOperation: You cannot call this from an async context - use a thread or sync_to_async.
WebSocket DISCONNECT /ws/videochat/ [127.0.0.1:38392]

consumer.py The codes are in a pastebin because here gives error for a lot of code: Show code

How can I solve this? and I should say that the tutorial was okay and that coder made it run.

Author:Nimda,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/62048253/error-in-asyncconsumer-in-django-channels
yy