Make cherrypy.engine not global anymore

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Make cherrypy.engine not global anymore

Sylvain Hellegouarch

Hi All,

I've been banging my head around a simple (yet probably not common) use
case where I wanted to fork a process and run a CherryPy instance from
that child process.

I quickly realized that the child process shared (as far as I could tell)
the same cherrypy.engine has its parent, which isn't what I wanted of
course.

I think this is a result of having cherrypy.engine being set to the module
level WSPBus instance in cherrypy.process. This is also the result of
cherrypy various attributes to be set at the module level in
cherrypy.__init__

I was therefore wondering if it wouldn't make some sense to lazily bind
cherrypy.engine and other attributes when accessed for the first time.
Something along:

_engine = None
def setup_engine(bus=None):
    if not bus:
       bus = WSPBus()

    # more initializations

    return bus

def _get_engine():
    if not _engine:
       _engine = setup_engine()
    return _engine

def _set_engine(engine):
    _engine = engine

engine = property(_get_engine, _set_engine)

Now, this snippet is entirely not tested and might break in various ways
but it's to illustrate the idea.

That would also mean we can import other CherryPy sub-packages without the
engine being initialized no matter what.

I often use the cherrypy.process without even setting up a CherryPy
application, hence not even starting cherrypy.engine.

This also means, one could "reset" the engine instance.

Food for thoughts,
- Sylvain


--
Sylvain Hellegouarch
http://www.defuze.org

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "cherrypy-devel" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/cherrypy-devel
-~----------~----~----~----~------~----~------~--~---