[Twisted-Python] 2 questions: adbapi.ConnectionPool ; Defer.DeferredSemaphore , threads.deferToThread

Jonathan Vanasco twisted-python at 2xlp.com
Fri Sep 15 01:06:37 EDT 2006

>> a-
>>     can someone clarify adbapi.ConnectionPool for me?
>>     i'm specifically wondering if connect/disconnect returns a  
>> handle to the pool and it the pool blocks until a handle is  
>> ready.  i think it probably doesn't work that way.  but i feel the  
>> need to check.

>> I can only answer your first question. Connect/disconnect are  
>> blocking
> functions and are generally only called from the connection pool  
> itself.
> User code generally only needs to call runInteraction, runQuery, or
> runOperation, and the pool will make connections as needed.

hm... i'll have to play with this.  i'm trying to fork some already  
written scripts that block as threads... grabbing a lock on a db from  
the pool, then returning it when done.

i answered part b for me.

i don't know if this example below would be of use to anyone, and its  
definitely poorly written.
but it is both functional and example of running a bunch of blocking  
processes in a finite number of threads managed by DeferredSemaphore  
( i could only find docs/examples for infinite threads or non- 
threaded DeferredSemaphore )


from twisted.application import internet
from twisted.internet import defer , reactor , threads

import time


class ThreadedSemaphoreService(object):
	semaphore= defer.DeferredSemaphore( tokens= SIMULTANEOUS )

	def __init__(self):

	def interval_action( self ):
		if HELP:
			print "interval_action()"
		# this would likely be a sql select
		action_queue= range(1,6)

		if len ( action_queue ):
			actions= []
			for action_item in action_queue:
				d= self.act_on( action_item )
			finished= defer.DeferredList( actions )
			finished.addCallback( self.re_register__interval_action )

	def re_register__interval_action( self , deferlist ):
		if HELP:
			print "re_register__interval_action()"
		reactor.callLater( CHECK_PERIOD , self.interval_action )
	def act_on( self , queue_item ):
		if HELP:
			print "in main - act_on()"
		t= threads.deferToThread( self._act_on )
		return t

	def _act_on( self ):
		if HELP:
			print "\t thread - _act_on()"
		d= self.semaphore.acquire()
		d.addCallback( self.sleeper )
		d.addCallback( self.awake )

	def sleeper( self , deferredSemaphore ):
		if HELP:
			print "\t thread - sleeper() - i want to sleep in my own thread  
		return deferredSemaphore

	def awake( self , deferredSemaphore ):
		if HELP:
			print "\t thead - awake() - now awake"

if __name__ == '__main__':
	myService= ThreadedSemaphoreService()

More information about the Twisted-Python mailing list