Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

use gather to execute_many #474

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 6 additions & 3 deletions databases/backends/aiopg.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import asyncio
import getpass
import json
import logging
Expand Down Expand Up @@ -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()

Expand Down
9 changes: 6 additions & 3 deletions databases/backends/asyncmy.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import asyncio
import getpass
import logging
import typing
Expand Down Expand Up @@ -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()

Expand Down
9 changes: 6 additions & 3 deletions databases/backends/mysql.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import asyncio
import getpass
import logging
import typing
Expand Down Expand Up @@ -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()

Expand Down
5 changes: 4 additions & 1 deletion databases/backends/postgres.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import asyncio
import logging
import typing

Expand Down Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions databases/backends/sqlite.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import asyncio
import logging
import typing
import uuid
Expand Down Expand Up @@ -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
Expand Down