如何从Bottle Asynchronous Primer实现'SomeAsyncWorker()'?

Dav*_*rth 5 python asynchronous gevent bottle

我有一堆长时间运行的脚本,它们执行一些数字运算,并且当它们通过打印运行写入输出到控制台时我想从浏览器调用这些脚本,并在浏览器运行时显示进度.我正在玩瓶子,正在通过这个引物http://bottlepy.org/docs/dev/async.html#这是相当整洁的.

我想尝试事件回调http://bottlepy.org/docs/dev/async.html#event-callbacks,因为这似乎完全符合我的问题,该脚本将作为AsyncWorker运行(理想情况下由某些消息队列管理)限制在任何一个实例上运行的数量)并定期回写它的状态.但我无法弄清楚SomeAsyncWorker()是什么 - 它是龙卷风类还是我要实现的gevent类还是别的什么?

@route('/fetch')
def fetch():
    body = gevent.queue.Queue()
    worker = SomeAsyncWorker()
    worker.on_data(body.put)
    worker.on_finish(lambda: body.put(StopIteration))
    worker.start()
    return body
Run Code Online (Sandbox Code Playgroud)

ron*_*man 0

(不完全是您问题的答案,但这是您可以采取的另一种策略。)

我拼凑了一个非常简单的多线程 WSGI 服务器,非常适合放在瓶子里。这是一个例子:

import bottle
import time
from mtbottle import MTServer

app = bottle.Bottle()

@app.route('/')
def foo():
    time.sleep(2)
    return 'hello, world!\n'

app.run(server=MTServer, host='0.0.0.0', port=8080, thread_count=3)

# app is nonblocking; it will handle up to 3 requests concurrently.
# A 4th concurrent request will block until one of the first 3 completed.
Run Code Online (Sandbox Code Playgroud)

https://github.com/RonRothman/mtwsgi

一个缺点是该端口上的所有端点都将是异步的;相反,gevent 方法(我认为)可以让您更好地控制哪些方法是异步的,哪些是同步的。

希望这可以帮助!