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
As soon as synchronous code considered "critical" by Django (database operations with DjangoORM for the most part) is used during an asynchronous code cycle, Django will raise a SynchronousOnlyOperation: You cannot call this from an async context - use a thread or sync_to_async error.
At that point, in the reference, we had option to set the settings to DJANGO_ALLOW_ASYNC_UNSAFE=True. Doing so make the whole process to work. But this poses a problem, as this deactivated barrier can cause data loss or corruption on other async.
Another option would have been to turn Storage synchronous calls to the session into asynchronous calls to the methods that require them. But your client is not adapted for using async methods inside this class, and we would need to create a new LogtoClient to do so.
Python SDK with Django session async issue
Having recently discovered logto.io, we tried to implement it in Django.
We tried using TraditionalWebApp process to protect our views, following your official Flask example.
Implementation
First, I implemented
/signin
and/callback
routes as shown in the tutorial, wrapping the logto package logic with async_to_sync decorators.To follow the tutorial, I used session out of views using Django ref : https://docs.djangoproject.com/en/5.0/topics/http/sessions/#using-sessions-out-of-views
views.py
urls.py
Problem
As soon as synchronous code considered "critical" by Django (database operations with DjangoORM for the most part) is used during an asynchronous code cycle, Django will raise a
SynchronousOnlyOperation: You cannot call this from an async context - use a thread or sync_to_async
error.Reference on this error : https://docs.djangoproject.com/en/5.0/topics/async/#async-safety
At that point, in the reference, we had option to set the settings to
DJANGO_ALLOW_ASYNC_UNSAFE=True
. Doing so make the whole process to work. But this poses a problem, as this deactivated barrier can cause data loss or corruption on other async.Another option would have been to turn Storage synchronous calls to the session into asynchronous calls to the methods that require them. But your client is not adapted for using async methods inside this class, and we would need to create a new LogtoClient to do so.
settings.py
Therefore, my question is : did anyone try TraditionalWebApp process with Django ? Did you find a way to make it work ?
Thanks in advance for your insights, and thanks for all the work you already did there !
The text was updated successfully, but these errors were encountered: