我正在 python 和扭曲的框架中创建一个聊天守护程序。而且我想知道当连接多个用户时,我是否必须删除函数中创建的每个变量以从长远来看节省内存,或者这些变量是否会自动清除?这是我的代码的精简版本来说明我的观点:
class Chat(LineOnlyReceiver):
LineOnlyReceiver.MAX_LENGTH = 500
def lineReceived(self, data):
self.sendMessage(data)
def sendMessage(self, data):
try:
message = data.split(None,1)[1]
except IndexError:
return
self.factory.sendAll(message)
#QUESTION : do i have to delete message and date??????????????????
del message
del data
class ChatFactory(Factory):
protocol = Chat
def __init__(self):
self.clients = []
def addClient(self, newclient):
self.clients.append(newclient)
def delClient(self, client):
self.clients.remove(client)
def sendAll(self, message):
for client in self.clients:
client.transport.write(message + "\n")