1 year ago

#270377

test-img

Scottie

ThreadPoolExecutor is not monkey-patched to the Gevent's one

I want to execute some long-lived tasks asynchronously by submitting them into a thread pool.

However, gevent's monkey patching does not do the trick for from concurrent.futures import ThreadPoolExecutor. (It should replace the built-in thread pool to gevent.threadpool.ThreadPoolExecutor)

Here is my main.py:

from concurrent.futures import ThreadPoolExecutor
# from gevent.threadpool import ThreadPoolExecutor
from flask import Flask

app = Flask("playground")

@app.route("/test")
def test_route():
    ThreadPoolExecutor().submit(is_prime, 2147483647)
    return "ok"

def is_prime(n):
    for i in range(2, n):
        if n % i == 0:
            print(f"{n} is NOT a prime number")
            return
    print(f"{n} is a prime number!!")
  • Run the application by gunicorn --timeout=5 --worker-class=gevent main:app
  • Trigger the API by curl localhost:8000/test

My is-prime job will be interrupted due to gevent worker timeout (5 seconds).

Everything will be fine when I uncomment the second line.

Am I missing something with the gevent's monkey patching? Thanks for any suggestions.

python

flask

threadpool

gevent

monkeypatching

0 Answers

Your Answer

Accepted video resources