Streaming Connection Using Python Bottle, Multiprocessing, and gevent

最后都变了- 提交于 2019-12-01 18:38:53

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")
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!