0

I'm writing a server for sending rendered images to clients (browsers) using Flask. Since an image maybe not ready (is still rendering), I wrote the following code to wait for a rendering thread until it finishes.

@app_autoView.route("/autoviewimgs/<origin>/<identifier>")
def autoviewimgs(origin, identifier):
    if identifier in renderThreads:
        renderThreads[identifier].join()
    return flask.send_from_directory(f'./latentspace/autoview/{origin}', identifier + '.png')

However, it seems the entire server start to wait for a single thread, and all other HTTP requests are blocked...

I don't know if python-threading is suitable for Flask or alternative ways of doing this...

I have tried Celery as suggested by @ParthS007, but the server was still blocked, as shown in the following code:

@app_autoView.route("/autoviewimgs/<origin>/<identifier>")
def autoviewimgs(origin, identifier):
    if identifier in renderThreads:
        if not renderThreads[identifier].ready():
            casename = renderThreads[identifier].get()
    return flask.send_from_directory(f'./latentspace/autoview/{origin}', identifier + '.png')
2
  • 2
    Don't use threads in web application. When run in the development server they will block, in a production setup they might get kicked by the WSGI server at any time. Commented Jun 22, 2021 at 10:27
  • Thanks so much. Are there any alternative ways to do something like threading in Flask? Commented Jun 22, 2021 at 10:32

1 Answer 1

1

It sounds like, you're trying to receive one request from the user and then have the app use threading. For this, use something like celery where you can give tasks to worker threads.

Read more about design in Flask

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.