run cherrypy with my own main loop?

893 views
Skip to first unread message

illume

unread,
Jun 3, 2008, 12:59:12 AM6/3/08
to cherrypy-users
Hi,

I'd like to run cherrypy, and keep my main loop... is this possible?

eg,

import cherrypy

class HelloWorld(object):
def index(self):
return "Hello World!"
index.exposed = True

cherrypy.quickstart(HelloWorld())
# returns here, rather than running forever.

while 1:
# do my own stuff
do_my_stuff_here = 1

# this is the method I want to know about?
cherrypy.iterate()


Or maybe I can run cherrypy is a separate thread somehow?



cheers,

Lakin Wecker

unread,
Jun 3, 2008, 10:55:18 AM6/3/08
to cherryp...@googlegroups.com
Cherrypy already runs in a separate thread, so you can do this. Here
is an example using CP 3.1, if you're using an older version of
CherryPy there may be a few other steps aside from mounting the
application and telling the engine to start.:
import cherrypy

class HelloWorld(object):
def index(self):
return "Hello World!"
index.exposed = True

cherrypy.tree.mount(HelloWorld(), "/")
cherrypy.engine.start()

# Put your main loop in a try finally to ensure that we tell
# cherrypy to stop when we're done (even if we have an unhandled exception).
# This is important, or it will hang your process when your
# application doesn't exit properly.
try:
for x in range(15):
print "doing my own main loop", x
import time
time.sleep(1) # sleep so that we can confirm the server is running

finally:
print "I'm done, and am now exiting."
cherrypy.engine.stop()


Lakin

illume

unread,
Jun 3, 2008, 7:26:36 PM6/3/08
to cherrypy-users
hi,

thank you.

cherrypy.engine.start(blocking=False) is how you get it to not block
in 3.0.

It looks like the API is broken for cherrypy.engine.start(blocking =
False) in 3.1

cu,

Robert Brewer

unread,
Jun 3, 2008, 7:46:43 PM6/3/08
to cherryp...@googlegroups.com
illume wrote:
> cherrypy.engine.start(blocking=False) is how you get it to not block
> in 3.0.
>
> It looks like the API is broken for cherrypy.engine.start(blocking =
> False) in 3.1

Not broken, just different. In 3.1 you make two calls: engine.start(),
then engine.block(). That change was made in part to make what you're
doing more explicit, not to mention more obvious that the feature is
both there and desirable.


Robert Brewer
fuma...@aminus.org

Lakin Wecker

unread,
Jun 3, 2008, 8:26:05 PM6/3/08
to cherryp...@googlegroups.com
The API has changed, yes.

I don't think it's broken.

Lakin

illume

unread,
Jun 4, 2008, 2:16:49 AM6/4/08
to cherrypy-users
cool.

here's a work around for the api change... if you want it to work with
different versions of cherrypy.

def engine_block(engine):
if hasattr(engine.block):
engine.block()
else:
engine.start(blocking=True)

def engine_start(engine):
if hasattr(engine.block):
engine.start()
else:
engine.start(blocking=False)


cheers,
> fuman...@aminus.org

illume

unread,
Jun 4, 2008, 2:21:54 AM6/4/08
to cherrypy-users
... and updated to actually work.

def engine_block(engine):
if hasattr(engine, "block"):
engine.block()
else:
engine.start(blocking=True)

def engine_start(engine):
if hasattr(engine, "block"):
engine.start()
else:
engine.start(blocking=False)
Reply all
Reply to author
Forward
0 new messages