我正在尝试建立一个在后台处理渲染的Web服务,这需要一分钟。在渲染过程中,我希望服务器能够并行处理请求,并返回Id {} not found
或结果(如果有的话)。
我发现的教程主要处理简单的请求而无需进行大量处理(http://bottlepy.org/docs/dev/async.html,使用sleep
来模拟处理)。因此,我不太确定如何实现线程处理-是否应在线程中设置瓶颈路径?
从http://bottlepy.org/docs/dev/tutorial_app.html#server-setup起,我知道bottlepy的默认服务器是单线程的,因此我尝试切换到另一台服务器(PasteServer)。
from bottle import Bottle, run, PasteServer
from service import startWithDirectArgs, default_out, default_out_dir
import threading
class BakingThread(threading.Thread):
# lock = threading.Lock()
isProcessRunning = False
resultDict = {}
currentId = 0
def __init__(self, bakingId: str, args):
super().__init__()
self.bakingId = bakingId
self.args = args
def run(self):
# with BakingThread.lock:
if BakingThread.isProcessRunning:
return False
BakingThread.processRunning = True
print("\033[1;32;49m" +
"Starting baking process with id {}".format(self.bakingId) +
"\033[0;37;49m")
result = startWithDirectArgs(self.args)
# result = calculatePi(100_0000_00)
BakingThread.resultDict[self.bakingId] = str(result)
BakingThread.isProcessRunning = False
print("\033[1;32;49m" +
"Finished baking process with id {}".format(self.bakingId) +
"\033[0;37;49m")
return result
def getUniqueId() -> str:
BakingThread.currentId += 1
return str(BakingThread.currentId)
def calculatePi(n: int) -> float:
halfPi = 1.0
zaehler = 2.0
nenner = 1.0
for i in range(n):
halfPi *= zaehler / nenner
if i % 2:
zaehler += 2.0
else:
nenner += 2.0
return 2.0 * halfPi
app = Bottle()
@app.route("/bakeFile/<fileParam>")
def bakeFile(fileParam: str):
# args = {"file": fileParam, "out": default_out_dir + default_out}
args = {
"file": "build/igmodels/AOMaps/Scene.igxc", # fileParam,
"out": default_out_dir + default_out
}
print(args)
cid = getUniqueId()
bt = BakingThread(cid, args)
bt.start()
bt.join()
@app.route("/bakeUrl/<urlParam>")
def bakeUrl(urlParam: str):
args = {"url": urlParam, "out": default_out_dir + default_out}
print(args)
cid = getUniqueId()
bt = BakingThread(cid, args)
bt.start()
bt.join()
@app.route("/pullState/<bakingId>")
def pullState(bakingId: str):
print("\033[1;33;49m" + "pullState id {}".format(BakingThread.currentId) +
"\033[0;37;49m")
result = BakingThread.resultDict.get(bakingId,
"Id {} not found".format(bakingId))
return result
app.run(host="localhost", port=8080, debug=True, server=PasteServer)
我希望能够运行http://localhost:8080/bakeFile/3dGeometryFileName
,并且在运行渲染时,我希望调用http://localhost:8080/pullState/1
以Id 1 not found
进行响应。渲染完成后,相同的调用应返回结果。
编辑:渲染过程是在C ++中实现的,并与PyBind绑定。全局解释器锁定(GIL)阻止了同时执行渲染和Web服务,因此我在C ++代码中的昂贵计算之前添加了py::gil_scoped_release release;
,之后又添加了py::gil_scoped_acquire acquire;
。在上面的代码中,我添加了一个代码段来直接在不使用C ++ / PyBind的python中计算pi,因此bottlePy开发人员可以将我引向该GIL问题。 (Thx Marcel)
答案 0 :(得分:0)
解决了。
渲染过程是在C ++中实现的,并与PyBind绑定。全局解释器锁定(GIL)阻止了同时执行渲染和Web服务,因此我在C ++代码中的昂贵计算之前添加了py::gil_scoped_release release;
,之后又添加了py::gil_scoped_acquire acquire;
。在上面的代码中,我添加了一个代码段来直接在不使用C ++ / PyBind的python中计算pi,因此bottlePy开发人员可以将我引向该GIL问题。 (Thx Marcel)