我有一個(gè)消費(fèi)者class BingoConsumer(WebsocketConsumer): logged_in = 0 def connect(self): async_to_sync(self.channel_layer.group_add)( "login", self.channel_name ) self.accept() def disconnect(self, close_code): async_to_sync(self.channel_layer.group_discard)( "login", self.channel_name ) self.logged_in -= 1 def receive(self, text_data): text_data = json.loads(text_data) if text_data['type'] == 'login': self.logged_in += 1 async_to_sync(self.channel_layer.group_send)( "login", { 'type': 'login', 'count': self.logged_in, } ) def login(self, event): self.send(text_data=json.dumps({ 'type': 'login', 'total': event['count'], }))每次用戶登錄我的網(wǎng)站時(shí)都會(huì)調(diào)用它,它會(huì)自動(dòng)調(diào)用類型為“登錄”的 websocket。我想跟蹤當(dāng)前登錄的用戶數(shù)量,但目前無(wú)論有多少人登錄,它仍然是 1。這讓我想知道,Django 是否將消費(fèi)者視為單一使用類?它會(huì)根據(jù)需要?jiǎng)?chuàng)建它們并銷毀它們嗎?
1 回答

POPMUISE
TA貢獻(xiàn)1765條經(jīng)驗(yàn) 獲得超5個(gè)贊
不同會(huì)話之間不共享變量。如果你想在多個(gè)會(huì)話中計(jì)算變量,你可能需要使用數(shù)據(jù)庫(kù)或本地文件。
添加回答
舉報(bào)
0/150
提交
取消