I have a Bottle application that uses subprocesses to do most of the work for requests. For routes that return a single response, I do something like what\'s below.
In your last example, worker.doStuff()
returns a generator, which is iterable. You can just return that (change yield
to return
). Bottle accept iterables as return values, as long es they yield byte or unicode strings.
After a lot of research and experimenting, I've determined that a gevent queue can't be used with Python multiprocessing in this way. Instead of doing things this way, something like redis can be used to allow the processes and gevent greenlets communicate.
@route('/stream')
def index():
worker = multiprocessing.Process(target=do_stuff)
worker.start()
yield redis_server.lpop()
def do_stuff(body):
while True:
gevent.sleep(5)
redis_server.lpush("data")