# async\_to\_sync(channel\_layer.group\_send) CPU and Memory Usage

**URL:** <https://forum.djangoproject.com/t/async-to-sync-channel-layer-group-send-cpu-and-memory-usage/27817>\
**Category:** Async/Channels\
**Created:** [February 6, 2024, 4:56pm UTC](https://forum.djangoproject.com/t/async-to-sync-channel-layer-group-send-cpu-and-memory-usage/27817 "2024-02-06T16:56:44Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aroxed](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/aroxed/32/19000_2.png) [@Aroxed](https://forum.djangoproject.com/u/Aroxed)\
**Post date:** [February 6, 2024, 4:56pm UTC](https://forum.djangoproject.com/t/async-to-sync-channel-layer-group-send-cpu-and-memory-usage/27817/1 "2024-02-06T16:56:44Z")

</div>

Hi everyone. I use django-channels for playing PCM audio data retrieved from UDP-server. It works, but sound is played with 1 second delay and what is more suspicious is that sending sound data to a web browser takes 10% of the CPU. It’s growing memory usage constantly (though after the audio finishes playing memory is released).  
The traffic is approximately 40 packets per second, and the length of a packet is 400 bytes. It was tested using InMemoryChannelLayer and RedisChannelLayer.

Other actions occurring between data received from UDP and sent to websockets (channel\_layer.group\_send) don’t affect CPU and memory. To test this I just commented out async\_to\_sync(channel\_layer.group\_send). It showed at most 1% of CPU usage.

Here is the code of sending:

```auto
        channel_layer = get_channel_layer()
        private_room_name = ConsumerUtils().get_private_channel_name(chat_handler.ui_client.username)
        async_to_sync(channel_layer.group_send)(
            private_room_name,
            {
                'type': 'chat_message',
                'message': sound_bytes
            }
        )

```

and the consumer:

```auto
class TheConsumer(WebsocketConsumer):
..............................
    def chat_message(self, event):
        """
        It sends voice data to a client
        :param event: a voice message as bytes
        """
        self.send(bytes_data=event['message'])

```

I’d appreciate any hints or suggestions.  
Thanks in advance.  
Andrii.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [February 6, 2024, 5:14pm UTC](https://forum.djangoproject.com/t/async-to-sync-channel-layer-group-send-cpu-and-memory-usage/27817/2 "2024-02-06T17:14:59Z")

</div>

`<conjecture>`  
My gut reaction to this is that you absolutely want to make sure that your listener that is receiving this data _and_ your consumer are all pure async. The overhead of starting up event loops when necessary is likely to be quite significant, and the more consumers you have rebroadcasting this data, the worse it’s going to be.  
`</conjecture>`

---

<div class="post-metadata">

**Author:** ![Aroxed](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/aroxed/32/19000_2.png) [@Aroxed](https://forum.djangoproject.com/u/Aroxed)\
**Post date:** [February 6, 2024, 5:48pm UTC](https://forum.djangoproject.com/t/async-to-sync-channel-layer-group-send-cpu-and-memory-usage/27817/3 "2024-02-06T17:48:45Z")

</div>

Thanks for the quick reply. You mentioned: “The overhead of starting up event loops when necessary is likely to be quite significant”. Could you explain at which moment the event loop is starting: when a new WebSocket client is connected or when I call group\_send?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [February 6, 2024, 5:58pm UTC](https://forum.djangoproject.com/t/async-to-sync-channel-layer-group-send-cpu-and-memory-usage/27817/4 "2024-02-06T17:58:28Z")

</div>

It’s the async\_to\_sync method that will create a new event loop if it needs to.

Yes, it is in the statement at:

> [@Aroxed](#):
>
> `async_to_sync(channel_layer.group_send)(`

But it’s in the `async_to_sync` call and _not_ the `group_send` function.

---

<div class="post-metadata">

**Author:** ![Aroxed](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/aroxed/32/19000_2.png) [@Aroxed](https://forum.djangoproject.com/u/Aroxed)\
**Post date:** [February 6, 2024, 6:07pm UTC](https://forum.djangoproject.com/t/async-to-sync-channel-layer-group-send-cpu-and-memory-usage/27817/5 "2024-02-06T18:07:36Z")

</div>

Do you think there’s a good solution? Would Async-consumer help?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [February 6, 2024, 6:13pm UTC](https://forum.djangoproject.com/t/async-to-sync-channel-layer-group-send-cpu-and-memory-usage/27817/6 "2024-02-06T18:13:17Z")

</div>

That’s what I’m recommending:

> [@KenWhitesell](#):
>
> you absolutely want to make sure that your listener that is receiving this data _and_ your consumer are all pure async.

It’s my opinion (and that’s all it is - I don’t have a way at the moment to reasonably test this) that by making your consumer async you’re going to reduce the “friction” that exists by switching between threaded and event-loop code.

Now, if you’ve got some sync-specific code that needs to be called by that consumer, that’s a different issue. You _could_ be creating that same friction, but in reverse. In _that_ situation, I’d be looking at a different solution.

I’d also be looking at a different way of structuring this:

> [@Aroxed](#):
>
> ```auto
> channel_layer = get_channel_layer()
> private_room_name = ConsumerUtils().get_private_channel_name(chat_handler.ui_client.username)
> async_to_sync(channel_layer.group_send)(
> private_room_name,
> {
> 'type': 'chat_message',
> 'message': sound_bytes
> }
> )
> 
> ```

If you’re calling this 40 times / second, there’s a lot of repeated calls being made to get the channel layer and private room name. (But without seeing the complete block of code, it’s difficult for me to offer an alternative.)

At an absolute minimum, I would make all consumers join a common group. Then, your sending code sends that data to that group, allowing Channels to route the data through to the individual consumers.

---

<div class="post-metadata">

**Author:** ![Aroxed](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/aroxed/32/19000_2.png) [@Aroxed](https://forum.djangoproject.com/u/Aroxed)\
**Post date:** [February 6, 2024, 7:07pm UTC](https://forum.djangoproject.com/t/async-to-sync-channel-layer-group-send-cpu-and-memory-usage/27817/7 "2024-02-06T19:07:31Z")

</div>

Thanks for the valuable advice!  
Since I need to send data only to one client I tried to store the consumer when connected in the user-related object and send data directly as follows:

```auto
chat_handler.ui_client.consumer.send(bytes_data=sound_bytes)

```

The CPU usage decreased to 3-5%, memory usage did not increase.  
Acceptable result!

Again, thanks a million.
