Search code examples
pythonwebserverpython-multithreadingbottle

How to set up bottlepy with threading for processing in background


I'm trying to set up a web service that processes a rendering in background, which takes a minute. While the rendering is in progress i want the server to be able to handle requests in parallel, returning Id {} not found or the result if there is one.

The tutorials i found mainly handle simple requests without much processing (http://bottlepy.org/docs/dev/async.html, using sleep to emulate processing). So i'm not quite sure how to implement threading - should the bottlepy routes be set up in a thread?

From http://bottlepy.org/docs/dev/tutorial_app.html#server-setup i know that the default server of bottlepy is single-threaded so i tried to switch to another server (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)

I expect to be able to run http://localhost:8080/bakeFile/3dGeometryFileName and while the rendering is running i expect calling http://localhost:8080/pullState/1 to respond with Id 1 not found. After the rendering is done the same call should return a result.

Edit: The rendering process was implemented in C++, bound with PyBind. The Global Interpreter Lock (GIL) prevented the concurrent execution of rendering and webserving, so i added py::gil_scoped_release release; before and py::gil_scoped_acquire acquire; after the expensive calculations in the C++ code. In the code above i added a snippet to calculate pi directly in python without C++/PyBind, so the bottlePy developer could point me onto that GIL thing. (Thx Marcel)


Solution

  • Solved it.

    The rendering process was implemented in C++, bound with PyBind. The Global Interpreter Lock (GIL) prevented the concurrent execution of rendering and webserving, so i added py::gil_scoped_release release; before and py::gil_scoped_acquire acquire; after the expensive calculations in the C++ code. In the code above i added a snippet to calculate pi directly in python without C++/PyBind, so the bottlePy developer could point me onto that GIL thing. (Thx Marcel)