Root object spawned twice when using Application fixture

shapeshifter avatarshapeshifter created an issue

No matter what I try, cherryd seems to spawn my root object twice. Here's the whole testcase demoed: http://pastie.org/3086988

It's really just one file problem.py:

import cherrypy

class Problem(object):
    def __init__(self):
        print self

with this cherrypy_config:

[global]
tree.problem = cherrypy.Application(problem.Problem())
server.socket_host = '0.0.0.0'
server.socket_port = 8090
tools.encode.encoding = 'utf-8'
tools.encode.on = True 
log.error_file = "/tmp/cp_errors.log"

[/]
tools.sessions.on = False

and then running the application like this:

cherryd -c cherrypy_config 

And the problem is that the Problem() object is created twice:

<problem.Problem object at 0xa37da2c>
[28/Dec/2011:23:11:17] ENGINE Mounted: cherrypy._cptree.Application(<problem.Problem object at 0xa37da2c>, '') on /
<problem.Problem object at 0xa37df4c>
[28/Dec/2011:23:11:17] ENGINE Listening for SIGHUP.
[28/Dec/2011:23:11:17] ENGINE Listening for SIGTERM.
[28/Dec/2011:23:11:17] ENGINE Listening for SIGUSR1.
[28/Dec/2011:23:11:17] ENGINE Bus STARTING
[28/Dec/2011:23:11:17] ENGINE Started monitor thread '_TimeoutMonitor'.
[28/Dec/2011:23:11:17] ENGINE Started monitor thread 'Autoreloader'.
[28/Dec/2011:23:11:18] ENGINE Serving on 0.0.0.0:8090
[28/Dec/2011:23:11:18] ENGINE Bus STARTED
^C[28/Dec/2011:23:11:21] ENGINE Keyboard Interrupt: shutting down bus
[28/Dec/2011:23:11:21] ENGINE Bus STOPPING
[28/Dec/2011:23:11:21] ENGINE HTTP Server cherrypy._cpwsgi_server.CPWSGIServer(('0.0.0.0', 8090)) shut down
[28/Dec/2011:23:11:21] ENGINE Stopped thread 'Autoreloader'.
[28/Dec/2011:23:11:21] ENGINE Stopped thread '_TimeoutMonitor'.
[28/Dec/2011:23:11:21] ENGINE Bus STOPPED
[28/Dec/2011:23:11:21] ENGINE Bus EXITING
[28/Dec/2011:23:11:21] ENGINE Bus EXITED
[28/Dec/2011:23:11:21] ENGINE Waiting for child threads to terminate...
[straydog@saskatoon testcase]$

And the whole problem with this is that whatever BackgroundTask I start in my root object gets started twice and that's not good at all. Here's a pdb run if it helps: http://pastie.org/3095136 The objects are created on line 330 and 347 of the paste. I tried working around the problem by using slightly different configurations, e.g.

import cherrypy

class Problem(object):
    def __init__(self):
        print self
app = cherrypy.Application(Problem())

and

[global]
tree.problem = problem.app
server.socket_host = '0.0.0.0'
server.socket_port = 8090
tools.encode.encoding = 'utf-8'
tools.encode.on = True 
log.error_file = "/tmp/cp_errors.log"

[/]
tools.sessions.on = False

and this works for this specific test case, but in my application where some more stuff is going on, it actually get spawned 4 times with this configuration. For my application it looks like this:

[straydog@saskatoon server]$ cherryd -c cherrypy_config 
<facets.Facets object at 0x916d4ec>
<facets..Facets object at 0x917514c>
<facets.Facets object at 0x9140bec>
[30/Dec/2011:13:01:31] ENGINE Mounted: cherrypy._cptree.Application(<facets.Facets object at 0x9140bec>, '') on /
<facets.Facets object at 0x9140cac>
[30/Dec/2011:13:01:31] ENGINE Listening for SIGHUP.
[30/Dec/2011:13:01:31] ENGINE Listening for SIGTERM.
[30/Dec/2011:13:01:31] ENGINE Listening for SIGUSR1.
[30/Dec/2011:13:01:31] ENGINE Bus STARTING
[30/Dec/2011:13:01:31] ENGINE Started monitor thread '_TimeoutMonitor'.
[30/Dec/2011:13:01:31] ENGINE Started monitor thread 'Autoreloader'.
[30/Dec/2011:13:01:31] ENGINE Serving on 0.0.0.0:8090
[30/Dec/2011:13:01:31] ENGINE Bus STARTED

In any case, even in this specific test case, I don't see why it should be spawned twice.

I tried the 3.2.0 and svn versions.

Comments (2)

  1. Anonymous

    This isn't a bug. There are two configs in cherrypy: the global config and the application config. If you only provide one, cherryd uses the provided config as the global and then merges that same config into the application config. Since you are instantiating Problem in the config, when that config is inserted twice, it is instantiated twice.

    The easiest solution is to instantiate your app outside of the config, then provide that instance in the config:

    Add the line "app = Problem()" to problem.py

    Change the line in the config to look like this: tree.problem = cherrypy.Application(problem.app)

    Then import your module on runtime with cherryd, like this: cherryd -i problem -c cherrypy_config

    sprawl$ cherryd -i problem -c cherrypy_config <problem.Problem object at 0x7d09636c> [05/Jan/2012:13:31:36] ENGINE Mounted: cherrypy._cptree.Application(<problem.Problem object at 0x7d09636c>, '') on / [05/Jan/2012:13:31:36] ENGINE Listening for SIGHUP. [05/Jan/2012:13:31:36] ENGINE Listening for SIGTERM. [05/Jan/2012:13:31:36] ENGINE Listening for SIGUSR1. [05/Jan/2012:13:31:36] ENGINE Bus STARTING [05/Jan/2012:13:31:36] ENGINE Started monitor thread '_TimeoutMonitor'. [05/Jan/2012:13:31:36] ENGINE Started monitor thread 'Autoreloader'. [05/Jan/2012:13:31:36] ENGINE Serving on 0.0.0.0:8090 [05/Jan/2012:13:31:36] ENGINE Bus STARTED

  2. Log in to comment
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.