PythonのフレームワークであるFastAPIを触る機会があって、その中で非同期プログラミングの存在を知りました。 非同期プログラミングについて色々調べていくと、DB接続も非同期処理に対応してきていることが分かり、PythonのSQLライブラリで有名なSQLAlchemyもバージョン1.4から非同期処理に対応しているようです。 時代は非同期プログラミングみたいですね。 ただSQLAlchemyの非同期処理の記事がまだ少なく、同期処理から非同期処理に変えるときに、苦労しそうなので、今回はSQLAlchemy同期処理と非同期処理で設定やクエリがどう違うの比較しようと思います。 SQLAlchemyにはcoreとormがありますが、ここではormで書いていきます。 APIにはFastAPI、DBはPostgreSQLを使っていきます。 動作確認用コードは下記リポジトリに置いています。 同期処理