[Twisted-Python] adbapi question
Hi Below is a small script. My question is what is the correct way to exit after using the adbapi stuff. The script as it stands just hangs the terminal until I Ctrl-Z and kill it. sample run jon> python simpledb.py jon jon jon Done 0 finished {1026: <connection object at 0x8228048>} 0 [<_MainThread(MainThread, started)>, <Thread(PoolThread-136416140-1, started)>] Jon import sys import threading import pprint from twisted.enterprise import adbapi from twisted.internet import reactor dbname,user,passwd = sys.argv[1:4] class SomeTable(adbapi.Augmentation): def getSomeData(self): self.done = 1 qry = "select * from SomeTable" return self.runQuery(qry).addCallbacks(self.operationDone,self.operationError) def operationDone(self, done): print "Done" self.done =0 def operationError(self,err): print err dbpool = adbapi.ConnectionPool("psycopg","dbname=%s user=%s" % (dbname,user)) db1=SomeTable(dbpool) db1.getSomeData() while 1 == db1.done: reactor.iterate() print db1.done print "finished" reactor.crash() reactor.stop() dbpool.close() print dbpool.connections print reactor.running print threading.enumerate()
Someone will surely correct me if I'm wrong, but I ran into this same dilemma when I began to use twisted. It seems that as twisted is an event-driven framework for asynchronous networked applications, it isn't intended to be used in this way. Twisted applications should generally sit around waiting for requests, and respond to them in a timely, but non-blocking manner. Starting up, doing some stuff, and exiting, just doesn't seem to be the idiom at play here. YMMV.. On Wed, 2003-04-30 at 17:28, Jon Dyte wrote:
Hi
Below is a small script. My question is what is the correct way to exit after using the adbapi stuff. The script as it stands just hangs the terminal until I Ctrl-Z and kill it. sample run
jon> python simpledb.py jon jon jon Done 0 finished {1026: <connection object at 0x8228048>} 0 [<_MainThread(MainThread, started)>, <Thread(PoolThread-136416140-1, started)>]
Jon
import sys import threading import pprint from twisted.enterprise import adbapi from twisted.internet import reactor
dbname,user,passwd = sys.argv[1:4]
class SomeTable(adbapi.Augmentation): def getSomeData(self): self.done = 1 qry = "select * from SomeTable" return self.runQuery(qry).addCallbacks(self.operationDone,self.operationError)
def operationDone(self, done): print "Done" self.done =0
def operationError(self,err): print err
dbpool = adbapi.ConnectionPool("psycopg","dbname=%s user=%s" % (dbname,user))
db1=SomeTable(dbpool)
db1.getSomeData()
while 1 == db1.done: reactor.iterate()
print db1.done print "finished" reactor.crash() reactor.stop() dbpool.close() print dbpool.connections print reactor.running print threading.enumerate()
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
On 30 Apr 2003 20:29:01 -0500 Justin Ryan <justin@gnubia.net> wrote:
Someone will surely correct me if I'm wrong, but I ran into this same dilemma when I began to use twisted. It seems that as twisted is an event-driven framework for asynchronous networked applications, it isn't intended to be used in this way.
Dunno what you mean by "this way". If you need to deal with a blocking database operation you dispatch it to the thread pool, and that works just fine. -- Itamar Shtull-Trauring http://itamarst.org/ http://www.zoteca.com -- Python & Twisted consulting
Well, I think it was a misconception on my part.. It does seem as if the reactor is not _designed_ to be used in short 'scripts' like this one, but I wasn't aware you could call reactor.stop(). On Wed, 2003-04-30 at 20:39, Itamar Shtull-Trauring wrote:
On 30 Apr 2003 20:29:01 -0500 Justin Ryan <justin@gnubia.net> wrote:
Someone will surely correct me if I'm wrong, but I ran into this same dilemma when I began to use twisted. It seems that as twisted is an event-driven framework for asynchronous networked applications, it isn't intended to be used in this way.
Dunno what you mean by "this way". If you need to deal with a blocking database operation you dispatch it to the thread pool, and that works just fine.
On 02 May 2003 09:33:38 -0500 Justin Ryan <justin@gnubia.net> wrote:
Well, I think it was a misconception on my part.. It does seem as if the reactor is not _designed_ to be used in short 'scripts' like this one, but I wasn't aware you could call reactor.stop().
Not true. All of the examples in doc/examples/ are short scripts. Apparently you just aren't using it correctly. You just need to understand how event driven programming works. Once you make that conceptual leap, it's all pretty trivial. -- Itamar Shtull-Trauring http://itamarst.org/ http://www.zoteca.com -- Python & Twisted consulting
On Wed, 30 Apr 2003 23:28:31 +0100 Jon Dyte <jon@totient.demon.co.uk> wrote:
Below is a small script. My question is what is the correct way to exit after using the adbapi stuff. The script as it stands just hangs the terminal until I Ctrl-Z and kill it.
You don't want a reactor.crash() and a reactor.stop(). Just do reactor.stop(). Using threads can make your python program not exit if they hang around, but reactor.stop() should shut down all threads in the thread pool. -- Itamar Shtull-Trauring http://itamarst.org/ http://www.zoteca.com -- Python & Twisted consulting
On Thursday 01 May 2003 2:38 am, Itamar Shtull-Trauring wrote:
On Wed, 30 Apr 2003 23:28:31 +0100
Jon Dyte <jon@totient.demon.co.uk> wrote:
Below is a small script. My question is what is the correct way to exit after using the adbapi stuff. The script as it stands just hangs the terminal until I Ctrl-Z and kill it.
You don't want a reactor.crash() and a reactor.stop(). Just do reactor.stop().
Using threads can make your python program not exit if they hang around, but reactor.stop() should shut down all threads in the thread pool. I added just reactor.stop(), but it doesnt work. eg :- $ python simpledb.py jon jon jon Done 0 finished {1026: <connection object at 0x82282c0>} [<_MainThread(MainThread, started)>, <Thread(PoolThread-136416444-1, started)>]
The thread isnt removed from the threadpool. I get the same problem with the Sybase DB-API module as well as psycopg The script is import sys import threading import pprint from twisted.enterprise import adbapi from twisted.internet import reactor dbname,user,passwd = sys.argv[1:4] class SomeTable(adbapi.Augmentation): def getSomeData(self): self.done = 1 qry = "select * from SomeTable" return self.runQuery(qry).addCallbacks(self.operationDone,self.operationError) def operationDone(self, done): print "Done" self.done =0 def operationError(self,err): print err dbpool = adbapi.ConnectionPool("psycopg","dbname=%s user=%s" % (dbname,user)) db1=SomeTable(dbpool) db1.getSomeData() while 1 == db1.done: reactor.iterate() print db1.done print "finished" reactor.stop() print dbpool.connections print threading.enumerate() Jon
On Thu, 1 May 2003 22:35:44 +0100 Jon Dyte <jon@totient.demon.co.uk> wrote:
while 1 == db1.done: reactor.iterate()
This is also wrong. You should be doing reactor.run(). -- Itamar Shtull-Trauring http://itamarst.org/ http://www.zoteca.com -- Python & Twisted consulting
It may be worthwhile to point out that reactor.run() should replace that while loop.. each callback could check if db1.done == 1, and if it doesn't, call reactor.stop().. I think.. On Thu, 2003-05-01 at 17:41, Itamar Shtull-Trauring wrote:
On Thu, 1 May 2003 22:35:44 +0100 Jon Dyte <jon@totient.demon.co.uk> wrote:
while 1 == db1.done: reactor.iterate()
This is also wrong. You should be doing reactor.run().
participants (3)
-
Itamar Shtull-Trauring
-
Jon Dyte
-
Justin Ryan