0

We have a Flask App which extensively uses threading.Thread class to spawn threads for Async API requests. Sample flask app that imitates our application is as below.

from flask import Flask import threading

app = Flask(__name__)

@app.route('/') def hello():
    return 'Hello, World!'

@app.route('/count') def count():
    def do_work():
        for i in range(5):
            print(f'Processing item {i}')
            # Perform your work here

    # Create and start a new thread
    thread = threading.Thread(target=do_work)
    thread.start()

    return 'Work started in a separate thread!'

if __name__ == '__main__':
    app.run()

Running this Flask App(Flask-1.1.2) using Gunicorn(gunicorn-20.0.4) and Gevent worker class(gevent-21.1.2 and greenlet-0.4.17) on Python3.7.7 works fine. But upgrading to Python3.9, the same code base has issue around starting threads (Thread.start()). Below is the stack trace when it fails.

thread.start()
 File “/var/python/lib/python3.9/threading.py”, line 899, in start
  _start_new_thread(self._bootstrap, ())
 File “/var/python/lib/python3.9/site-packages/gevent-21.1.2-py3.9-freebsd-11.4-RELEASE-amd64.egg/gevent/thread.py”, line 82, in start_new_thread
  greenlet = Greenlet.spawn(function, *args)
 File “src/gevent/greenlet.py”, line 687, in gevent._gevent_cgreenlet.Greenlet.spawn
AttributeError: type object ‘method’ has no attribute ‘start’

Tried upgrading gevent-21.1.2 and greenlet-0.4.17 to next latest versions to see if that solves the problem. But the issue remains the same.

Community
  • 1
  • 1
Chethu2288
  • 11
  • 4

0 Answers0