Multiple modules with database access + general app design?

Robin Haswell rob at digital-crocus.com
Thu Jan 19 15:01:25 CET 2006


On Thu, 19 Jan 2006 14:37:34 +0100, Daniel Dittmar wrote:

> Robin Haswell wrote:
>> cursor for every class instance. This application runs in a very simple
>> threaded socket server - every time a new thread is created, we create a
>> new db.cursor (m = getattr(modules, module)\n m.c = db.cursor() is the
>> first part of the thread), and when the thread finishes all its actions
>> (of which there are many, but all sequential), the thread exits. I don't
> 
> 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()

?

> 
>> separate connection, but I get the feeling that a lot of cursors = a lot
>> of connections. I'd much prefer each method call with a thread to reuse
>> that thread's connection, as creating a connection incurs significant
>> overhead on the MySQL server and DNS server.
> 
> You can create several cursor objects from one connection. There should 
> be no problems if you finish processing of one cursor before you open 
> the next one. In earlier (current?) versions of MySQL, only one result 
> set could be opened at a time, so using cursors in parallel present some 
> problems to the driver implementor.
> 
> Daniel




More information about the Python-list mailing list