RE: [Twisted-Python] How to accept connections faster
Hi Mike, Thanks for the tip. My code is indeed running faster now. However, the reason for the loop was to somehow simulate late responses from a remote database server. I'm guessing I should put the database query code in the closeInABit method you mentioned like this : def closeInABit (): mydb = MySQLdb.Connect(db='mydatabase', host="10.10.10.10", user="myuser", passwd="mypassword") cursor = mydb.cursor() retval = '' cursor = mydb.cursor() cursor.execute("SELECT * FROM Employees WHERE empnum = 'ABCD'") retval = cursor.fetchone() if retval == SomeValue: self.transport.write("Login Ok") else: self.transport.write("Login not Ok") cursor.close() mydb.close() self.transport.loseConnection() Is this ok ? Or should there be some special Asycn coding technique I must also learn ? Regards, Danny ================================================================== Mike C. Fletcher wrote : Danny, Twisted is an asynchronous system. You *cannot* use that kind of blocking delay tactic in Twisted without totally destroying Twisted's functionality:
for i in range(10000000): pass print "Looping done." self.transport.loseConnection()
should have been something like: def closeInABit( ): self.transport.loseConnection() reactor.callLater( 2, closeInABit ) # call after 2 seconds You'll want to read up on asynchronous programming (e.g. on the Twisted web site) to get a feel for how this model of programming networked apps works. Good luck, Mike Sinang, Danny wrote:
Hello.
I ran the (Twisted) server code below and it accepts a client socket connection every 1.3 seconds. The equivalent Synchronous socket code accepts one every 2 seconds.
Can anyone here suggest any code improvements for my Twisted server to
accept connections faster ?
I've include the Synchronous socket server code and the client generator code below as well.
... ________________________________________________ Mike C. Fletcher Designer, VR Plumber, Coder http://www.vrplumber.com http://blog.vrplumber.com PyCon is coming...
On Thu, 17 Mar 2005 09:25:58 +0800, "Sinang, Danny" <d.sinang@spitech.com> wrote:
Hi Mike,
Thanks for the tip. My code is indeed running faster now.
However, the reason for the loop was to somehow simulate late responses from a remote database server.
I'm guessing I should put the database query code in the closeInABit method you mentioned like this : [snip - blocking database code]
Is this ok ? Or should there be some special Asycn coding technique I must also learn ?
Yea, async. Async is nice. Twisted comes with a moduled named "adbapi" that provides a non-blocking layer around standard DB-API 2.0 modules (such as MySQLdb). You can read more about it here at <http://twistedmatrix.com/documents/current/howto/enterprise>. Jp
Jp Calderone wrote:
Yea, async. Async is nice. Twisted comes with a moduled named "adbapi" that provides a non-blocking layer around standard DB-API 2.0 modules (such as MySQLdb). You can read more about it here at <http://twistedmatrix.com/documents/current/howto/enterprise>.
Keep in mind adbapi is just a wrapper for synchronous libraries that uses threading. Ideally you would write asynchronous clients for the SQL databases -- I've been thinking about doing just that for MySQL. That is, I've been thinking, NOT doing.
Keep in mind adbapi is just a wrapper for synchronous libraries that uses threading. Ideally you would write asynchronous clients for the SQL databases -- I've been thinking about doing just that for MySQL. That is, I've been thinking, NOT doing.
There is an async adapter for PostgreSQL, PGasync: http://www.jamwt.com/pgasync/ [Flames about MySQL deleted. ;-) ] -- Nicola Larosa - nico@tekNico.net ...this particular abuse of the rules of procedure will make a great textbook example for the lack of democracy in the EU. While the Luxembourg Presidency quoted some vague "institutional reasons" for their position, their reasoning actually serves to discredit the institution of the Council, and the whole EU project with it. -- Karl-Friedrich Lenz, http://k.lenz.name/wordpress/index.php?p=26
participants (4)
-
Jp Calderone -
Ken Kinder -
Nicola Larosa -
Sinang, Danny