If you would like to serve your flask application using gevent, there are two small settings you will need to add.
Instruct turboduck to store connection information in a thread local:
# app configuration
DATABASE = {
'name': 'my_db',
'engine': 'turboduck.PostgresqlDatabase',
'user': 'postgres',
'threadlocals': True, # <-- this
}
Some time before instantiating a Database object (and preferrably at the very “beginning” of your code) you will want to monkey-patch the standard library thread module:
from gevent import monkey; monkey.patch_thread()
If you want to patch everything (recommended):
from gevent import monkey; monkey.patch_all()
Note
Remember to monkey-patch before initializing your app
flask-turboduck opens a connection-per-request. Flask stores things, like “per-request” information, in a special object called a context local. Flask will ensure that this works even in a greened environment. turboduck does not automatically work in a “greened” environment, and stores connection state on the database instance in a local. turboduck can use a thread local instead, which ensures connections are not shared across threads. When using turboduck with gevent, it is necessary to make this “threadlocal” a “greenlet local” by monkeypatching the thread module.