Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing Twisted AMP with Database insertion
    primarykey
    data
    text
    <p>I am learning how to use Twisted AMP. I am developing a program that sends data from a client to a server and inserts the data in a SQLite3 DB. The server then sends back a result to the client which indicates success or error (try and except might not be the best way to do this but it is only a temporary solution while I work out the main problem). In order to do this I modified an example I found that originally did a sum and returned the result, so I realize that this might not be the most efficient way to do what I am trying to do. In particular I am trying to do some timings on multiple insertions (i.e. send the data to the server multiple times for multiple insertions) and I have included the code I have written. It works but clearly it is not a good way to send multiple data for insertion since I am performing multiple connections before running the reactor.</p> <p>I have tried several ways to get around this including passing the ClientCreator to reactor.callWhenRunning() but you cannot do this with a deferred.</p> <p>Any suggestions, advice or help with how to do this would be much appreciated. Here is the code.</p> <p>Server:</p> <pre><code>from twisted.protocols import amp from twisted.internet import reactor from twisted.internet.protocol import Factory import sqlite3, time class Insert(amp.Command): arguments = [('data', amp.Integer())] response = [('insert_result', amp.Integer())] class Protocol(amp.AMP): def __init__(self): self.conn = sqlite3.connect('biomed1.db') self.c =self.conn.cursor() self.res=None @Insert.responder def dbInsert(self, data): self.InsertDB(data) #call the DB inserter result=self.res # send back the result of the insertion return {'insert_result': result} def InsertDB(self,data): tm=time.time() print "insert time:",tm chx=data PID=2 device_ID=5 try: self.c.execute("INSERT INTO btdata4(co2_data, patient_Id, sensor_Id) VALUES ('%s','%s','%s')" % (chx, PID, device_ID)) except Exception, err: print err self.res=0 else: self.res=1 self.conn.commit() pf = Factory() pf.protocol = Protocol reactor.listenTCP(1234, pf) reactor.run() </code></pre> <p>Client:</p> <pre><code>from twisted.internet import reactor from twisted.internet.protocol import ClientCreator from twisted.protocols import amp import time class Insert(amp.Command): arguments = [('data', amp.Integer())] response = [('insert_result', amp.Integer())] def connected(protocol): return protocol.callRemote(Insert, data=5555).addCallback(gotResult) def gotResult(result): print 'insert_result:', result['insert_result'] tm=time.time() print "stop", tm def error(reason): print "error", reason tm=time.time() print "start",tm for i in range (10): #send data over ten times ClientCreator(reactor, amp.AMP).connectTCP( '127.0.0.1', 1234).addCallback(connected).addErrback(error) reactor.run() </code></pre> <p>End of Code.</p> <p>Thank you.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload