如何在uwsgi中延迟start_response?

3 投票
1 回答
691 浏览
提问于 2025-04-18 18:13

我想做以下事情:

r = requests.post('https://foo.com/test', data=json.dumps(fields), headers=headers)
if r.status_code != requests.codes.ok:
    start_response(str(r.status_code) + ' ' + r.reason, [('Content-Type', 'text/plain')])
    body.put(r.reason)
else:
    start_response('200 OK', [('Content-Type', 'application/json')])
    body.put(r.json())

但是这样调用会让主线程卡住,所以我这样做:

body = queue.Queue()
gevent.spawn(make_request, environ, start_response, body)

可是现在我遇到了一个错误:SystemError: 你只能在主可调用对象中调用uwsgi API函数

那么,我该如何在POST请求完成后再延迟处理start_response的结果呢?

1 个回答

2

你可以直接阻塞线程——只需启动多个uWSGI实例:

uwsgi --http :9090 --wsgi-file foobar.py --master --processes 4 --threads 2

来自 https://uwsgi-docs.readthedocs.org/en/latest/WSGIquickstart.html#adding-concurrency-and-monitoring

撰写回答