You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is your feature request related to a problem? Please describe.
I can't work with ormar in concurrent contexts very well since databases^0.8, because databases has changed (my doing in encode/databases#546) to not share connections across asyncio tasks. Databases will now create a new connection for each asyncio.Task. For me this is a problem when I want to do some concurrent work within a transaction.
Describe the solution you'd like
A clear and concise description of what you want to happen.
I'd like to be able to specify a databases connection to use for a given operation. Something like this would be great:
The gist is that by running this work in separate asyncio tasks, databases creates a new connection, and in the context of that new connection, there's no active transaction and since that csse department is not yet committed, we get a foreign key error.
# except from complete example in the file aboveasyncwithdb:
asyncwithdb.transaction():
csse=awaitDepartment(id=1337, name="Computer Science & Software Engineering").save()
courses= [ ... ]
asyncwithasyncio.TaskGroup() astasks:
forname, idincourses:
tasks.create_task(Course(id=id, name=name, department=csse).save())
Here I'm just turning the coroutine from .save() into a task as a placeholder for a real world example where there's IO bound work to be done for each "course", and it'd be significantly faster to do that concurrently.
If I could just tell ormar which connection to use, this wouldn't be a problem.
Note: I'm filing this as an enhancement rather than a bug, since [email protected] requires databases<0.6.3 and [email protected] requires databases^0.7.0, and connection use across tasks is fixed in [email protected]; so currently ormar doesn't experience this, but it will
The text was updated successfully, but these errors were encountered:
@zevisert good question. I need to have a look and come back to you. Thank you for tagging me on it. @devkral can we run some tests and check this, please?
@zevisert i can see I your example that you want to use different connections? We have something similar with the "using" and "using_with_db" operations on a queryset. We don't share connections per se but We have it working properly but this might be another underlying problem that might require some addressing.
@tarsil I haven't tested, but I think this may be an issue for you based on what I see in Edgy's docs. As of databases>=0.8.0, you have to provide a way to share the current connection to reuse the state of a transaction across asyncio.Tasks. Edgy's using seems to only provide the ability to refer to another registered database or schema by it's registered name; it doesn't seem like a databases.core.connection instance can be specified to Edgy.
Is your feature request related to a problem? Please describe.
I can't work with ormar in concurrent contexts very well since databases^0.8, because databases has changed (my doing in encode/databases#546) to not share connections across asyncio tasks. Databases will now create a new connection for each
asyncio.Task
. For me this is a problem when I want to do some concurrent work within a transaction.Describe the solution you'd like
A clear and concise description of what you want to happen.
I'd like to be able to specify a databases connection to use for a given operation. Something like this would be great:
Model.with(connection=connection).objects.get()
or,Model.objects.get(connection=connection)
or,Model(...).with(connection=connection).save()
, orModel(...).save(connection=connection)
Describe alternatives you've considered
A clear and concise description of any alternative solutions or features you've considered.
I don't have any alternative options right now at all.
Additional context
Add any other context or screenshots about the feature request here.
The MVCE below shows this.
To run it:
docker run -it --rm -e POSTGRES_PASSWORD=demo-1360 -p 5432:5432 postgres
in one terminalpython -m venv .demo-1360 && source .demo-1360/bin/activate
pip install ormar==0.12.2 asyncpg && pip install databases==0.8.0
See endnotepython ormar-connection.py --concurrent
(should error),python ormar-connection.py
won't errorSource for ormar-connection.py
The gist is that by running this work in separate asyncio tasks, databases creates a new connection, and in the context of that new connection, there's no active transaction and since that
csse
department is not yet committed, we get a foreign key error.Here I'm just turning the coroutine from
.save()
into a task as a placeholder for a real world example where there's IO bound work to be done for each "course", and it'd be significantly faster to do that concurrently.If I could just tell ormar which connection to use, this wouldn't be a problem.
Note: I'm filing this as an enhancement rather than a bug, since [email protected] requires databases<0.6.3 and [email protected] requires databases^0.7.0, and connection use across tasks is fixed in [email protected]; so currently ormar doesn't experience this, but it will
The text was updated successfully, but these errors were encountered: