Multiple modules with database access + general app design?

Daniel Dittmar daniel.dittmar at sap.corp
Thu Jan 19 14:37:34 CET 2006


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.

> 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