django.channels async consumer does not appear to

2019-06-14 13:51发布

I have added django.channels to a django project in order to support long running processes that notify users of progress via websockets.

Everything appears to work fine except for the fact that the implementation of the long running process doesn't seem to respond asynchronously.

For testing I have created an AsyncConsumer that recognizes two types of messages 'run' and 'isBusy'.

The 'run' message handler sets a 'busy flag' sends back a 'process is running' message, waits asynchronously for 20 seconds resets the 'busy flag' and then sends back a 'process complete message'

The 'isBusy' message returns a message with the status of the busy flag.

My expectation is that if I send a run message I will receive immediately a 'process is running' message back and after 20 seconds I will receive a 'process complete' message. This works as expected.

I also expect that if I send a 'isBusy' message I will receive immediately a response with the state of the flag.

The observed behaviour is as follows:

  • a message 'run' is sent (from the client)
  • a message 'running please wait' is immediately received
  • a message 'isBusy' is sent (from the client)
  • the message reaches the web socket listener on the server side
  • nothing happens until the run handler finishes
  • a 'finished running' message is received on the client
  • followed immediately by a 'process isBusy:False' message

Here is the implementation of the Channel listener:

class BackgroundConsoleConsumer(AsyncConsumer):
    def __init__(self, scope):
        super().__init__(scope)
        self.busy = False

    async def run(self, message):
        print("run got message", message)
        self.busy = True
        await self.channel_layer.group_send('consoleChannel',{
                    "type":"consoleResponse",
                    "text":"running please wait"
                })
        await asyncio.sleep(20)
        self.busy = False
        await self.channel_layer.group_send('consoleChannel',{
                    "type":"consoleResponse",
                    "text": "finished running"
                })

    async def isBusy(self,message):
        print('isBusy got message', message)
        await self.channel_layer.group_send('consoleChannel',{
                    "type":"consoleResponse",
                    "text":  "process isBusy:{0}".format(self.busy)
                })

The channel is set up in the routing file as follows:

application = ProtocolTypeRouter({
    "websocket": AuthMiddlewareStack(
        URLRouter([
            url("^console/$", ConsoleConsumer),
        ])

    ),
    "channel": ChannelNameRouter({
        "background-console":BackgroundConsoleConsumer,
    }),
})

I run the channel with one worker (via ./manage.py runworker ).

The experiment was done with the django test server (via runserver).

Any ideas as to why the channel consumer does not appear to work asynchronously would be appreciated.

1条回答
Summer. ? 凉城
2楼-- · 2019-06-14 14:29

After a bit of digging around here is the problem and one solution to it.

A channel adds messages sent to it to a asyncio.Queue and processes them sequentially.

It is not enough to release the coroutine control (via a asyncio.sleep() or something similar), one must finish processing the message handler before a new message is received by the consumer.

Here is the fix to the previous example that behaves as expected (i.e. responds to the isBusy messages while processing the run long running task)

Thank you @user4815162342 for your suggestions.

class BackgroundConsoleConsumer(AsyncConsumer):
    def __init__(self, scope):
        super().__init__(scope)
        self.busy = False

    async def run(self, message):
        loop = asyncio.get_event_loop()
        loop.create_task(self.longRunning())

    async def longRunning(self):
        self.busy = True
        await self.channel_layer.group_send('consoleChannel',{
                    "type":"the.type",
                    "text": json.dumps({'message': "running please wait", 'author': 'background console process'})
                })
        print('before sleeping')
        await asyncio.sleep(20)
        print('after sleeping')
        self.busy = False
        await self.channel_layer.group_send('consoleChannel',{
                    "type":"the.type",
                    "text": json.dumps({'message': "finished running", 'author': 'background console process'})
                })

    async def isBusy(self,message):
        print('isBusy got message', message)
        await self.channel_layer.group_send('consoleChannel',{
                    "type":"the.type",
                    "text":  json.dumps({'message': "process isBusy:{0}".format(self.busy),
                                         'author': 'background console process'})
                })
查看更多
登录 后发表回答