I am new to Twisted. I have many clients that constantly send data to the service, the server must insert this data into the database (Postgresql).
My problem is that my server is not behaving asynchronously:
for example: at t = 0, the client sends data that must be inserted into the database in 30 seconds, at t = 10: another client sends data to the server: this second client must wait 20 seconds (30-10) until the server inserts first customer data.
I want this data to be inserted asynchronously without waiting. A little help would be greatly appreciated. Here is my code:
from twisted.internet import reactor, protocol
from twisted.python import log
import sys,psycopg2
class Server(protocol.Protocol):
def dataReceived(self, data):
self.client_host = self.transport.getPeer().host
self.client_port = self.transport.getPeer().port
cursor=self.factory.connection.cursor()
self.factory.insert_data(data,self.client_host,cursor)
def connectionLost(self, reason):
log.msg('Connection lost from %s:%s.\n' % (self.client_host,str(self.client_port)))
class My_Factory(protocol.ServerFactory):
protocol = Server
def __init__(self):
params="host='127.0.0.1' port='5432' dbname='my_db' user='user' password='my_pwd'"
try:
self.connection= psycopg2.connect(params)
self.connection.autocommit = True
except Exception as e:
log.msg("Cannot connect to database!! Please verify connection params. Reason: %s"%str(e))
sys.exit(0)
def insert_data(self,data,adress,cursor):
try:
query = """INSERT INTO my_table(
data,client_adress, date)
VALUES (%s, %s, now());
"""%(data,adress)
cursor.execute(query)
except Exception as e:
log.msg("Error: "+str(e))
def main(argv):
log.startLogging(sys.stdout)()
my_factory=My_Factory()
reactor.listenTCP(8000,my_factory)
reactor.run()
if __name__ == '__main__':
main()
source
share