Monday, December 24, 2018

Re: Django channels communication not asynchronous

Sorry for that.... Actually in my code I did use await before send, but somehow got deleted when I posted here... I would try debugging it but I am having a doubt that it's because django is inherently event driven... Is there something like a request response cycle should be synchronous that you can't send responses in batches? Should I try to do it like conduct the execution in threads and the results being pushed to channel layer in the background and using django signals to trigger a call whenever something is pushed to the channel and send it to a websocket connection... But wouldn't that require two websocket connections? One for requesting and another for receiving responses.... Or am I going about it the wrong way....

On Monday, December 24, 2018 at 8:36:24 PM UTC+5:30, Andrew Godwin wrote:
You appear to have missed an "await" before "self.send" - could that be the cause?

In general, the best thing to do if things seem to "block" is to set the environment variable PYTHONASYNCIODEBUG=1 and Python will warn you if anything blocks for too long and seems synchronous.

Andrew

On Mon, Dec 24, 2018 at 2:24 PM Akshay Surya <aksh...@gmail.com> wrote:


Hi everybody,

I wrote django channels code to send api data from two different sources asynchronously through webscokets. The different sources takes few seconds to 1 minute to compute and send back the data. I managed to call them asynchronously using asyncio event loop. But the issue is that they are not sending the response back asynchronously. The code just waits for the all the data to arrive and sends everything at the same time.

Channels Code:

class SearchHotelConsumer(AsyncWebsocketConsumer):      def __init__(self, *args, **kwargs):          super().__init__(*args, **kwargs)          self.source1 = Source1()          self.source2 = Source2()        async def connect(self):          await self.accept()        def disconnect(self, close_code):          pass        async def _source1_handler(self, request, queue):          source1_response = await self.source1.async_post(request)            await queue.put(source1_response.data)        async def _source2_handler(self, request, queue):          source2_response = await self.source2.async_post(request)            await queue.put(source2_response.data)        async def _send_msg(self, queue):          while True:              message = await queue.get()              if message is None:                  break              print('got the message')              self.send(text_data=json.dumps({                  'message': message              }, cls=DjangoJSONEncoder))                queue.task_done()        def receive(self, text_data):          text_data_json = json.loads(text_data)          message = text_data_json['message']            request = HttpRequest()          request.method = 'POST'          request.session = self.scope["session"]          request = Request(request)          for key, val in message.items():              request.data[key] = val            queue = asyncio.Queue()          sender = asyncio.ensure_future(self._send_msg(queue))            await self._source1_handler(request, queue)          await self._source2_handler(request, queue)            await queue.join()            sender.cancel()
How can I make the message sending part truly asynchronous?

Regards

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAJXf11o_B7W3BiCpjKGEgEVynG2o5HoDRxY_EUFuJ%2B5Wbk5Yag%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/52b06816-ccc5-45bb-9358-0207a2aad513%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment