On Tue, May 20, 2008 at 2:07 PM, Mike Orr <[EMAIL PROTECTED]> wrote:
>> engine = engine_from_config(config, 'sqlalchemy.')
>> init_model(engine)
>
> This is done in environment.py, as shown in "The Engine" section of
> the article. It is not normally done in the base controller.
>
>> otherwise I get errors like:
>>
>> <class 'sqlalchemy.exceptions.ProgrammingError'>: (ProgrammingError)
>> SQLite objects created in a thread can only be used in that same
>> thread.The object was created in thread id -1224172656 and this is
>> thread id -1249350768 None [{}]
>
> SQLite does use one connection per thread, while other engines may
> give out multiple connections in one thread. You shouldn't have to
> use the "threadlocal" engine strategy; it's built into the SQLite
> engine.
>
> But I don't know why it would use the wrong connnection in a thread.
> Nobody else has reported this problem. When exactly does this occur,
> and what kinds of queries does your application contain?
I see the same kind of error when using postgres. As I understand it,
strategy='threadlocal' just allows you to share the connection per
*thread*, like you mentioned. However, the problem is that in
mod_wsgi when you are running a multi-threaded server your application
object is actually passed to each new thread, which then does
application(environ, start_response). As far as I can tell,
load_environment() is not run in each thread, only once when the
server starts up. To illustrate it, here is what my app.wsgi file
looks like:
import os
from paste.deploy import loadapp
import paste.script.appinstall
config = os.environ['PYLONS_CONFIG_PATH']
cmd = paste.script.appinstall.SetupCommand('setup-app')
cmd.run([config])
application = loadapp('config:' + config)
The fix is to create and bind a new engine object per __call__ in
BaseController and I suppose the thread of this email is whether or
not that's the right thing to do. Seems ok to me. You wouldn't want
the same engine object to be shared across each thread anyway since
simultaneous things would be happening in a heavily loaded site. A
secondary topic here might be whether or not the sqlalchemy docs
should be updated for this scenario. I'm not sure that a
multi-threaded mod_wsgi server setup is in the scope of that article
or not. I suppose it should be documented somewhere ;)
hmm, I just found this:
http://code.google.com/p/modwsgi/wiki/IntegrationWithPylons
... and noticed the suggested app.wsgi script does not explicitly call
'setup-app' like mine does. I have no idea where load_environment()
would be called in that case then.
K
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"pylons-discuss" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/pylons-discuss?hl=en
-~----------~----~----~----~------~----~------~--~---