Note that there are some explanatory texts on larger screens.

plurals
  1. POTwisted - Using a Deferred for another sql query
    primarykey
    data
    text
    <p>I've got a twisted based network application. Now I would like to implement a new database design but I''m getting stuck with the Deferred object.</p> <p>I write sessions into my database by using this two functions:</p> <pre><code>def createSession(self, peerIP, peerPort, hostIP, hostPort): SessionUUID = uuid.uuid1().hex yield self.writeSession(SessionUUID, peerIP, hostIP) sid = self.db.runQuery("SELECT id FROM sessions WHERE sid = %s", (SessionUUID,)) yield sid @defer.inlineCallbacks def writeSession(self, SessionUUID, peerIP, hostIP): sensorname = self.getSensor() or hostIP r = yield self.db.runQuery("SELECT id FROM sensors WHERE ip = %s", (sensorname,)) if r: id = r[0][0] else: yield self.db.runQuery("INSERT INTO sensors (ip) VALUES (%s)", (sensorname,)) r = yield self.db.runQuery("""SELECT LAST_INSERT_ID()""") id = int(r[0][0]) self.simpleQuery( """ INSERT INTO sessions (sid, starttime, sensor, ip) VALUES (%s, FROM_UNIXTIME(%s), %s, %s) """, (SessionUUID, self.nowUnix(), id, peerIP)) </code></pre> <p>In short words: createSession creates an UUID for the session and calls writeSession to write this into my db. After this is written I try to select the ID of the last insert by using the UUID in the where statement and return the result.</p> <p>Now my problem. To update the session information I call this function:</p> <pre><code>def handleConnectionLost(self, sid, args): self.simpleQuery("UPDATE sessions SET endtime = now() WHERE sid = %s", (sid)) </code></pre> <p>As you can see I try to use the sid from createSession which is an Deferred object and not an integer. If I got this right and I add a Callback to <code>handleConnectionLost</code> it will run the query at this time so that I can use the value here. But this is not my only function where I need the sid. So it would be an overhead when I do every time a callback when I need the sid.</p> <ol> <li>Is there a way that I can give my sid as an integer to my functions? So that I'm just running the query one time? How does it have to look like?</li> <li>When I'm using a Deferred query with a <code>now()</code> statement. Will it use <code>now()</code> when I added this query to my Callbacks or will it use <code>now()</code> when the query is fired?</li> </ol>
    singulars
    1. This table or related slice is empty.
    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.
 

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