我正在使用扭曲的应用程序服务器,我的流程连接是(我的代码遵循说明)
我正在使用pgbouncer,但是当我注意到模式句子上的pgbouncer时,问题是有许多连接被打开而只共享一个,我只需要使用一个共享连接,可能我需要使用类似于持久连接或者可能是从任何文件中使用var而不是通过vars的方式,看起来好像每个创建的var都是克隆打开的连接而不是共享连接
最后我的问题是:解释行为的原因是什么
非常感谢
path = None
config = ConfigParser.ConfigParser()
config.read('protocols.cfg')
conn = txpostgres.Connection()
defe = conn.connect("host=localhost dbname=gctracker user=gctracker password=PASSWORD port=6432")
application = service.Application("tracker")
factories = {}
for device in config.get('protocols', 'keys').split(','):
devicer = config.get(device, 'name')
factories[devicer] = TrackerFactory(devicer, conn, defe)
internet.TCPServer(int(config.get(device, 'port')), factories[devicer]).setServiceParent(application)
endpoint = TCP4ServerEndpoint(reactor, 8750)
factory = AMPFactory(factories)
ampService = StreamServerEndpointService(endpoint, factory)
ampService.setServiceParent(application)
from twisted.internet.protocol import Factory, Protocol
class TrackerServer(Protocol):
"""Tracker Twisted Protocol Class."""
def __init__(self, clients, devicer, connection, deferred):
self.clients = clients
self.devicer = devicer
self.connection = connection
self.deferred = deferred
self.decoder = None
self.host = None
self.peer = None
self.state = False
def connectionMade(self):
"""ConnectionMade Twisted event."""
try:
decoderModule = __import__('listener.protocols.%sDecoder' % (self.devicer, ), fromlist=['%sDecoder' % (self.devicer, )])
decoderClass = getattr(decoderModule, '%sDecoder' % (self.devicer, ))
self.decoder = decoderClass(self.connection, self.deferred)
self.peer = self.transport.getPeer()
self.host = self.transport.getHost()
self.decoder.openConnection(self.host.host, self.host.port, self.peer.host, self.peer.port)
print 'Connection made to', self.host, 'from', self.peer
self.clients[self.peer.host] = self
except ValueError:
print "Oops! Connection was not started"
def connectionLost(self, reason):
"""ConnectionLost Twisted event."""
if self.clients.has_key(self.peer.host):
del self.clients[self.peer.host]
self.decoder.closeConnection()
print "Connection lost from", self.peer.host, ':', reason
else:
print "Connection unknown peer:", reason
def dataReceived(self, data):
"""DataReceived Twisted event."""
#try:
""" how to precess here a line for a specific client"""
response = self.decoder.processDatagram(data)
if response != False:
self.sendMessage(response)
#d = threads.deferToThread(self.factory.decoder.processDatagram(data ))
#d.addCallback(self.sendResponse)
#except ValueError:
# print "Oops! That was no valid data. Try again..."
def sendMessage (self, response):
#print 'response', response
self.transport.write(response)
class TrackerFactory(Factory):
def __init__(self, devicer, connection, deferred):
self.clients = {}
self.devicer = devicer
self.connection = connection
self.deferred = deferred
def buildProtocol(self, addr):
proto = TrackerServer(self.clients, self.devicer, self.connection, self.deferred)
self.connectedProtocol = proto
return proto
class Decoder():
"""Decoder Metaclass
Decode datagrams according device protocol
"""
def __init__(self, connection, deferred):
"""Class Constructor"""
self._logId = None
self._id = None
self._imei = None
self._conn = connection
self._d = deferred
self.status = 0
self.processes = ()
self.connection = {
'ip': None,
'port': None,
'opend': None,
'closed': None,
'status': None
}
self.device = None
self.fields = {
'datagram': '',
'imei': '',
'date_time_process': None,
'latitude': 0.0,
'longitude': 0.0,
'course': 0,
'speed': 0,
'mileage': 0,
'gps_signal': 0,
'gsm_signal': 0,
'alarm_status': False,
'gps_status': False,
'vehicle_status': False,
'alarm_over_speed': False,
'other': '',
}
self.datagram = {
'bytes': None,
'header': None,
'length': None,
'type': None,
'stop': None,
'information': None,
}
self.validation = {
'header': None,
'tail': None,
}
self.types = {
'login': None,
'location': None,
'status': None,
'warning': None,
'alarm': None,
'gps': None,
'command': None,
}
self.response = ['00', '00']
def openConnection (self, host_ip, host_port, peer_ip, peer_port):
self.connection['host_ip'] = host_ip
self.connection['host_port'] = host_port
self.connection['peer_ip'] = peer_ip
self.connection['peer_port'] = peer_port
self.connection['status'] = True
self._d.addCallback(lambda _: self._conn.cursor())
self._d.addCallback(self.useCursorOnOpen)
def closeConnection (self):
self._d.addCallback(lambda _: self._conn.runQuery("select gps_close_connection(%s, %s);" % (self._logId, self._id)))
self._d.addCallback(lambda _: self._conn.close())
class PATROLURBANODecoder(Decoder):
'''
classdocs
'''
def __init__(self, connection, deferred):
Decoder.__init__(self, connection, deferred)
self.types['location'] = ''
self.response = None
self.sent = False
self.counter = 0