Search code examples
thread-safetypython-3.xsqlalchemycherrypy

CherryPy, SQLAlchemy Core thread safety?


in my web based app, i decided to use Cherrypy 3.2 as http framework.

I am using the cherrypy.Application class to create WSGI compatible appliaction object, which is served via Apache2 with mod_wsgi.

Also, I am using just core components of SQLalchemy 0.7.3 (not ORM). There are available some tools for cherrypy for correct session binding per request (like SATools). But Session object of SQLalchemy is part of the ORM, not the core.

So I have started thinking about how to make similar tool but without session.

The documentation of SQLalchemy says:

For a multiple-process application that uses the os.fork system call, or for example the Python multiprocessing module, it’s usually required that a separate Engine be used for each child process.

So how to correctly create one engine per cherrypy thread? Taking note that the threads are created by apache2 (probably).

Thank you!!

edit: it is maybe important, that wsgi application is ran in daemon mode by apache2


Solution

  • Under mod_wsgi I don't think this is an issue if I understand the question because the application isn't preloaded into memory prior to the fork in mod_wsgi. Instead, the application is separately loaded into each distinct process so there are no issues from shared stuff due to inheritance across a fork.