Streaming Connection Using Python Bottle, Multiprocessing, and gevent

前端 未结 2 1729
礼貌的吻别
礼貌的吻别 2021-01-18 06:39

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.

相关标签:
2条回答
  • 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.

    0 讨论(0)
  • 2021-01-18 07:22

    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")
    
    0 讨论(0)
提交回复
热议问题