1

After moving to channels2 I'm still struggling with python's "new" async/await and asyncio. First I tried to reproduce Worker and Background Tasks from the docs but then I realised that my task should just run as simple async function.

So, my test function is

async def replay_run(self, event):
    print_info("replay_run", event, self.channel_name)
    import asyncio
    for i in range(10):
        await asyncio.sleep(1)
        print_info("replay_run-",i, event, self.channel_name)

and both of the following calls inside async def receive_json(self, event) seem to prevent a subsequent incoming message from being handled right away.

Version 1:

await self.channel_layer.send(
    self.channel_name, {
        "type": "replay_run", "sessionID": msg["sessionID"]
    })

Version 2:

    await self.replay_run(msg)

First I thought of version 1 because I thought I needed to register a "new event consumer" like await asyncio.gather... Any hint on how to do this right is appreciated ...

Raja Simon
  • 10,126
  • 5
  • 43
  • 74
sebhaase
  • 564
  • 1
  • 6
  • 10
  • Are you saying your `print_info` not running as expected ? Or Did you receive any error ? Also What do you mean by not handling 2nd message before 1st finished ? – Raja Simon Jul 10 '18 at 19:21
  • No error, but I expected interleaving output of the two loops counting from 1 to 10. Instead the first counting finishes even before I see my 2nd incoming message and _then_ the 2nd counting from 1 to 10 after that – sebhaase Jul 11 '18 at 10:08

1 Answers1

0

Found the solution here: django.channels async consumer does not appear to execute asynchronously

Apparently the way would be

Version 3:

asyncio.ensure_future( self.replay_runit(msg) )

... in order to schedule the "long running coroutine" without blocking the channel.

sebhaase
  • 564
  • 1
  • 6
  • 10