0

我读了http://bottlepy.org/docs/dev/tutorial_app.html#server-setup

运行 Apache + Bottle + Python

Bottle + Apache + WSGI + 会话

我想知道是否可以运行异步rest api调用以在mod_wsgi服务器上运行到一个不返回任何内容(它的后端逻辑)并且是非阻塞的py函数 - 所以我查找了gevent但我没有找到了一个解决方案,您可以在其中使用 gevents 运行 mod_wsgi。

是否有任何解决方案可以使用 mod_wsgi 或任何其他替代方法在 apache 服务器上运行异步调用?

根据下面安德烈斯的回答进行更新;

我用瓶子+芹菜运行了一个简单的myip地址返回。所以必须将芹菜作为@celery.task 运行,然后运行(host='localhost', port=8080, debug=True)?是否也需要在终端上启动芹菜工人?在 [runnin local] 之前从未使用过 celery 也运行带有装饰器 @route(/something) 的瓶子,但 app.route 没有 where app = Bottle() 可能是由于某些 .wsgi 文件错误?

4

1 回答 1

1

抱歉,评论区放不下。每个请求最终都必须得到响应(或失败/超时)。如果您真的不需要向客户端返回任何数据,只需发送一个带有状态码的空响应即可。如果请求的处理需要时间,它应该异步运行,这就是 celery 的用武之地。因此,请求处理程序的阻塞实现:

def request_processor_long_running_blocking_func(request_data):
    # process request data, which takes a lot of time
    # result is probably written into db
    pass

def request_handler_func(request):
    request_processor_long_running_blocking_func(request.data)
    return HttpResponse(status=200)

如果我理解正确,这是您试图通过request_processor_long_running_blocking_func异步运行来避免的,因此request_handler_func不会阻塞。这可以像这样用芹菜解决:

from celery.task import task

@task
def request_processor_long_running_blocking_func(request_data):
    # the task decorator wraps your blocking function, with celery's Task class 
    # which has a delay method available for you to call, which will run your function
    # asynchronously on one of your celery background worker processes
    pass

def request_handler_func(request):
    request_processor_long_running_blocking_func.delay(request.data)
    # calling the function with delay won't block, it returns immediately
    # and your response is sent back instantly
    return HttpResponse(status=200)

还有一件事,用ajax发送这些任务请求,这样你的web界面就不会被重新加载或任何东西,所以用户可以在发送请求后继续使用你的应用程序

于 2013-08-15T12:22:48.720 回答