Skip to content

Instantly share code, notes, and snippets.

@sevdog
Created January 11, 2021 07:22
Show Gist options
  • Save sevdog/b70bb51bafff16a7a79630139d853b32 to your computer and use it in GitHub Desktop.
Save sevdog/b70bb51bafff16a7a79630139d853b32 to your computer and use it in GitHub Desktop.
Profile django-channels redis
import asyncio
import yappi
from channels.generic.websocket import AsyncWebsocketConsumer
yappi.set_clock_type("WALL")
class EchoProfileConsumer(AsyncWebsocketConsumer):
async def connect(self):
await self.accept()
#print(f'Connect {self.scope["client"]} to {self.channel_name}')
await self.channel_layer.group_add("test", self.channel_name)
async def disconnect(self, close_code):
await self.channel_layer.group_discard("test", self.channel_name)
async def receive(self, text_data=None, bytes_data=None):
with yappi.run():
await self.channel_layer.group_send(
"test",
{
"type": "chat.message",
"text": text_data
}
)
yappi.get_func_stats().print_all(columns={
0: ("name", 64), # need a long length to avoid trim
1: ("ncall", 5),
2: ("tsub", 8),
3: ("ttot", 8),
4: ("tavg", 8)
})
async def chat_message(self, event):
await self.send(event["text"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment