-
Notifications
You must be signed in to change notification settings - Fork 262
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’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unable to make concurrent queries with transactions #116
Comments
Could you expand this into a complete example? We got a pretty simple concurrency test case, so the first thing to progress this would be adding another test case demonstrating this failure case. |
Related to #134 @tomchristie I would really appreciate it if you could fix this problem. database = databases.Database(
url=postgresql_dsn,
force_rollback=True,
min_size=1,
max_size=2,
)
class TestConcurrentTransaction:
@pytest.fixture
async def db(self):
await database.connect()
yield
await database.disconnect()
@pytest.mark.asyncio
async def test_single_transaction(self, db):
await service_method()
@pytest.mark.asyncio
async def test_concurrent_transactions(self, db):
concurrent_num = 2
coros = []
for i in range(concurrent_num):
coro = service_method()
coros.append(coro)
await asyncio.gather(*coros)
@database.transaction()
async def service_method():
await repository_query_method()
async def repository_query_method():
sel = "SELECT CURRENT_TIMESTAMP"
row = await database.fetch_one(sel)
print(row['current_timestamp']) Error messages are as follows:
|
I'm running into the same issue. This is the simplest code I can come up with that demonstrates this error: import asyncio
import databases
db = databases.Database("postgresql://example:password@localhost/example")
async def query():
async with db.transaction():
await db.fetch_one("SELECT 1")
async def test():
await db.connect()
await query()
await asyncio.gather(query(), query())
if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.run_until_complete(test()) which causes the following exception
A few things to note:
|
Any progress on this open ticket? I am facing the same issue. |
I've outlined the underlying cause of this problem in #230. Fixing it requires a fundamental change to how the library stores concurrent connections - I think the existing mechanism using ContextVar is fundamentally broken. I've had to stop using |
Fellows, please check my workarounds: #176 (comment)
These two points guarantee that the workarounds will not break 😄 If somebody makes a PR from them, I'll happily merge it. |
I think #328 fixed this and I'll close this. |
Recently there was #81 resolved that enabled concurrent queries, but it only partially solves the problem. If queries are wrapped in transactions:
and executed in parallel the introduced lock doesn't work resulting in:
The text was updated successfully, but these errors were encountered: