Multiple modules with database access + general app design?

Daniel Dittmar daniel.dittmar at sap.corp
Thu Jan 19 15:43:58 CET 2006

Robin Haswell wrote:
> On Thu, 19 Jan 2006 14:37:34 +0100, Daniel Dittmar wrote:
>>If you use a threading server, you can't put the connection object into 
>>the module. Modules and hence module variables are shared across 
>>threads. You could use thread local storage, but I think it's better to 
>>pass the connection explicitely as a parameter.
> Would you say it would be better if in every thread I did:
> 	m = getattr(modules, module)
> 	b.db = db
> 	...
> 	def Foo():
> 		c = db.cursor()

I was thinking (example from original post):

	import modules ())

         # file
         def Bar (cursor):
             cursor.execute (...)

The same is true for other objects like the HTTP request: always pass 
them as parameters because module variables are shared between threads.

If you have an HTTP request object, then you could attach the database 
connection to that object, that way you have to pass only one object.

Or you create a new class that encompasses everything useful for this 
request: the HTTP request, the database connection, possibly an object 
containing authorization infos etc.

I assume that in PHP, global still means 'local to this request', as PHP 
probably runs in threads under Windows IIS (and Apache 2.0?). In Python, 
you have to be more explicit about the scope.


More information about the Python-list mailing list