6

i just learn about flask and requests. i want to get the number of working threads when i post request to the server. and i want to measure the time spent of the working threads. so this is my server and my client code:

server.py

from flask import Flask
from flask import request
import time 
from flaskthreads import AppContextThread

app = Flask(__name__)

@app.route("/", methods = ['GET', 'POST'])

def home():
    timeout = time.time() + 10   # 5 minutes from now

    while True:
        test = 0
        if test ==5 or time.time() > timeout:
            break 

    return 'Hello', 200

def main():
    app.run(host='0.0.0.0', threaded = True, debug = True)


if __name__ == "__main__":
    main()

client.py

import os
import requests
import glob
import time 
import base64


url = 'http://0.0.0.0:5000/'


def load_data():

    os.chdir('./500_mb')
    for image in glob.glob('*.jpg'):
        with open(image, 'rb') as imageFile:
            # image_s = base64.b64encode(imageFile.read())
            image_s = {'file_image':open(image, 'rb')}

    return image_s

def send_data():

    start = time.time()
    r = requests.post(url, files = load_data())
    end = time.time()

    print('client 1: {} ms'.format((end - start)*1000))



if __name__ == "__main__":
    send_data()

how do i know the number of working threads ? i just add threaded = True on the server. i've been searching the answer and none of them answer my question. thanks in advance !

0

1 Answer 1

12

First of all, the usual disclaimer: This is pretty much irrelevant because you would be able to specify this when you actually move to a deployment server.

However, I was curious myself, so I decided to trace this back.

The starting point is in app.run() so let's look at what that does:

def run_command(
    info, host, port, reload, debugger, eager_loading, with_threads, cert, extra_files
):

...

from werkzeug.serving import run_simple

    run_simple(
        host,
        port,
        app,
        use_reloader=reload,
        use_debugger=debugger,
        threaded=with_threads,
        ssl_context=cert,
        extra_files=extra_files,
    )

So, next port-of-call is werkzeug.serving with run_simple.

From here, you'll be buffeted about the module:

  1. First you'll see that this uses make_server
  2. make_server launches ThreadedWSGIServer
  3. ThreadedWSGIServer takes socketserver.ThreadingMixIn as an argument.

Head over to socketserver. We can see from the source code the following:

class ThreadingMixIn:
    """Mix-in class to handle each request in a new thread."""

    ...

    def process_request(self, request, client_address):
        """Start a new thread to process the request."""
        t = threading.Thread(target = self.process_request_thread,
                             args = (request, client_address))
        t.daemon = self.daemon_threads
        if not t.daemon and self.block_on_close:
            if self._threads is None:
                self._threads = []
            self._threads.append(t)
        t.start()

So, the answer is that it launches a new thread on each request. If nothing else, it's an interesting exercise in tracing code paths.


Edit in Sept 2021: I originally made the mistake of not getting permalinks from git, so different parts of the answer went out of sync with the code paths I was trying to show. I've since corrected that to try be more stable with permalinks but, with several libraries involved, do note that things could change at any time. I think the general principle will hold for a while, though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.