diff --git a/databases/backends/aiopg.py b/databases/backends/aiopg.py index 9ad12f63..d51b3255 100644 --- a/databases/backends/aiopg.py +++ b/databases/backends/aiopg.py @@ -1,3 +1,4 @@ +import asyncio import getpass import json import logging @@ -171,10 +172,12 @@ async def execute(self, query: ClauseElement) -> typing.Any: async def execute_many(self, queries: typing.List[ClauseElement]) -> None: assert self._connection is not None, "Connection is not acquired" cursor = await self._connection.cursor() + futures = [] + for single_query in queries: + single_query, args, context = self._compile(single_query) + futures.append(cursor.execute(single_query, args)) try: - for single_query in queries: - single_query, args, context = self._compile(single_query) - await cursor.execute(single_query, args) + await asyncio.gather(*futures) finally: cursor.close() diff --git a/databases/backends/asyncmy.py b/databases/backends/asyncmy.py index e15dfa45..c7ffb76b 100644 --- a/databases/backends/asyncmy.py +++ b/databases/backends/asyncmy.py @@ -1,3 +1,4 @@ +import asyncio import getpass import logging import typing @@ -161,10 +162,12 @@ async def execute(self, query: ClauseElement) -> typing.Any: async def execute_many(self, queries: typing.List[ClauseElement]) -> None: assert self._connection is not None, "Connection is not acquired" async with self._connection.cursor() as cursor: + futures = [] + for single_query in queries: + single_query, args, context = self._compile(single_query) + futures.append(cursor.execute(single_query, args)) try: - for single_query in queries: - single_query, args, context = self._compile(single_query) - await cursor.execute(single_query, args) + await asyncio.gather(*futures) finally: await cursor.close() diff --git a/databases/backends/mysql.py b/databases/backends/mysql.py index 2a0a8425..ef725d14 100644 --- a/databases/backends/mysql.py +++ b/databases/backends/mysql.py @@ -1,3 +1,4 @@ +import asyncio import getpass import logging import typing @@ -161,10 +162,12 @@ async def execute(self, query: ClauseElement) -> typing.Any: async def execute_many(self, queries: typing.List[ClauseElement]) -> None: assert self._connection is not None, "Connection is not acquired" cursor = await self._connection.cursor() + futures = [] + for single_query in queries: + single_query, args, context = self._compile(single_query) + futures.append(cursor.execute(single_query, args)) try: - for single_query in queries: - single_query, args, context = self._compile(single_query) - await cursor.execute(single_query, args) + await asyncio.gather(*futures) finally: await cursor.close() diff --git a/databases/backends/postgres.py b/databases/backends/postgres.py index 3e1a6fff..6f553405 100644 --- a/databases/backends/postgres.py +++ b/databases/backends/postgres.py @@ -1,3 +1,4 @@ +import asyncio import logging import typing @@ -221,9 +222,11 @@ async def execute_many(self, queries: typing.List[ClauseElement]) -> None: # asyncpg uses prepared statements under the hood, so we just # loop through multiple executes here, which should all end up # using the same prepared statement. + futures = [] for single_query in queries: single_query, args, result_columns = self._compile(single_query) - await self._connection.execute(single_query, *args) + futures.append(self._connection.execute(single_query, *args)) + await asyncio.gather(*futures) async def iterate( self, query: ClauseElement diff --git a/databases/backends/sqlite.py b/databases/backends/sqlite.py index 9626dcf8..c0049ace 100644 --- a/databases/backends/sqlite.py +++ b/databases/backends/sqlite.py @@ -1,3 +1,4 @@ +import asyncio import logging import typing import uuid @@ -137,8 +138,11 @@ async def execute(self, query: ClauseElement) -> typing.Any: async def execute_many(self, queries: typing.List[ClauseElement]) -> None: assert self._connection is not None, "Connection is not acquired" - for single_query in queries: - await self.execute(single_query) + futures = [ + self.execute(single_query) + for single_query in queries + ] + await asyncio.gather(*futures) async def iterate( self, query: ClauseElement