Open slyduda opened 4 years ago
After doing further testing I am noticing that I am not getting conflicts when nesting 2 db sessions within one session. The following code works for the second db_session function.
@db_session
async def create_account_db(email, username, hash, is_lead: bool):
user = User(
email=email, username=username, password=hash,
)
if is_lead:
user.lead = Lead(email=user.email)
The correct information is getting updated in the database. I found this a bit dissapointing as I was hoping to remove large chunks of redundant code by nesting smaller db_session functions like in my first example.
PROBLEM 2
Although async works if I don't nest, my first check_user function is not working and returns None while its synchronous counterpart returns the correct values.
@db_session
async def check_user(username, email):
is_username = User.exists(username=username)
is_email = User.exists(email=email)
return is_username or is_email
exists = await check_user(username, email)
print(exists) # PRINTS OUT NONE HERE IF ASYNC AND THE CORRECT VALUE IF SYNC.
Is this an issue with the cache bein wiped before returning the correct variable?
After some further testing, I have found that when the db_session wrapper is used with a async functions, the call property is not working correctly causing no value to be returned. I am still investigating.
I wouldn't expect this to work, best practice for using pony with async code can be found here:
https://docs.ponyorm.org/integration_with_fastapi.html#async-and-db-session
I'm getting an error when attempting to use nested async functions wrapped with the @db_session decorator. Here is my simplified code:
database.py
account.py
The error message that I get is as follow:
Edit: When I make everything ssynchronous everything works perfectly.