gpt4 book ai didi

multithreading - 使用 web.py 强制单线程请求处理

转载 作者:行者123 更新时间:2023-12-04 06:44:32 25 4
gpt4 key购买 nike

我正在使用 web.py 框架。出于调试目的,我想强制所有请求都由单个线程处理,或者使用互斥体模拟这种行为。我该怎么做?

最佳答案

让我提出这样的建议,但它只会在您的 Controller 方法上锁定当前应用程序堆栈。

import web
from threading import Lock

urls = ("/", "Index")


class Index:

def GET(self):
# This will be locked
return "hello world"


def mutex_processor():
mutex = Lock()

def processor_func(handle):
mutex.acquire()
try:
return handle()
finally:
mutex.release()
return processor_func

app = web.application(urls, globals())

app.add_processor(mutex_processor())

if __name__ == "__main__":
app.run()

UPD:如果您需要锁定整个应用程序堆栈,那么您可能必须使用您自己的 WSGI 中间件包装 app.wsgifunc。要了解想法,请查看我的答案 to this question .

关于multithreading - 使用 web.py 强制单线程请求处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13270257/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com