[英]access django channels' consumer class variables from outside
class ExampleConsumer(AsyncWebsocketConsumer):
async def connect(self):
self.id = 1
self.foo = 'bar'
await self.accept()
Is it possible to get all existing instances of ExampleConsumer, filter them by id
and get foo
value?是否可以获取 ExampleConsumer 的所有现有实例,按
id
过滤它们并获取foo
值? Somewhere in a django view django 视图中的某处
You can get all instance with gc.get_objects
so:您可以使用
gc.get_objects
获取所有实例,因此:
import gc
class ExampleConsumer(AsyncWebsocketConsumer):
async def connect(self):
self.id = 1
self.foo = 'bar'
await self.accept()
def get_inc(cls):
return [obj for obj in gc.get_objects() if isinstance(obj, cls)]
for i in get_inc(ExampleConsumer):
print(i.foo)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.