How can I use a Sync HTTP Consumer in Django Channels?

51 views
Skip to first unread message

Tal

unread,
Feb 8, 2021, 5:09:00 PM2/8/21
to Django users

I'm using Channels v2. I want to integrate long-polling into my project.

The only consumer I see in the documentation for http long polling is the AsyncHttpConsumer.

The code I need to run in my handle function is not asynchronous. It connects to another device on the network using a library that is not asynchronous. From what I understand, this will cause the event loop to block, which is bad. Client requests will be handled one at a time.

Can I run my handler synchronously, in a thread somehow?

I asked the same question here: https://stackoverflow.com/q/66055657/6423456, but so far got no responses.

I quickly wrote a subclass of SyncConsumer that works, but:

  1. I can't imagine that everyone is doing this - there must be an easier way
  2. The disconnect() method is not called until after the handle() method is done running, so from what I can tell, there's no way to interrupt the handle method early (unless the handle method kicks off its own thread or something)
I posted the code for the subclass of SyncConsumer as an answer to my stackoverflow question, but I'm hoping there's a better, easier way to do this.

Reply all
Reply to author
Forward
0 new messages