Skip to content

Instantly share code, notes, and snippets.

@pawl
Last active January 8, 2023 13:39
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pawl/0f0d87e7ee42f0a8eaf28f0d13629674 to your computer and use it in GitHub Desktop.
Save pawl/0f0d87e7ee42f0a8eaf28f0d13629674 to your computer and use it in GitHub Desktop.
test to see if psycogreen is necessary to run sqlalchemy + gevent + psycopg2 concurrently

This is a test to see if psycogreen is necessary to run sqlalchemy + gevent + psycopg2 concurrently.

benchmark command (concurrency of 10)

ab -n 10 -c 10 127.0.0.1:8080/wait

psychopg2 - sync worker (expecting concurrency of 2)

gunicorn -b 127.0.0.1:8080 -w 2 test:app

Requests per second: 1.99 [#/sec] (mean)

psychopg2 - gevent worker - test_no_psycogreen.py (expecting concurrency of 10)

gunicorn -b 127.0.0.1:8080 -w 2 test:app -k gevent

Requests per second: 1.98 [#/sec] (mean)

psychopg2 - gevent worker - test_psycogreen.py (expecting concurrency of 10)

gunicorn -b 127.0.0.1:8080 -w 2 test:app -k gevent

Requests per second: 9.42 [#/sec] (mean)

Results

Yes, psycogreen is required.

Flask
Flask-SQLAlchemy
gunicorn
gevent
psycopg2
sqlalchemy
psycogreen
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgres://localhost/postgres'
db = SQLAlchemy(app)
@app.route("/wait")
def wait():
db.session.execute('select pg_sleep(1.0)')
return "ok"
from psycogreen.gevent import patch_psycopg
patch_psycopg() # the only change
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgres://localhost/postgres'
db = SQLAlchemy(app)
@app.route("/wait")
def wait():
db.session.execute('select pg_sleep(1.0)')
return "ok"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment