-
-
Notifications
You must be signed in to change notification settings - Fork 311
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
Needs sync_to_async
in async context
#440
Comments
I came here to report something similar. This error also occurs if trying to use constance settings in views.py or urls.py (anything that gets called when Django starts up) when using the latest django-channels and deploying the app with asgi. An example would be using a constance config as a constant in a caching decorator. |
Same - django 3.1, python 3.7, latest constance |
Any thoughts? More details:
|
More complete workaround: from asgiref.sync import sync_to_async
from constance import config
@sync_to_async
def safe_constance_get(fld: str):
return getattr(config, fld)
z = safe_constance_get('Z') |
mea culpa, not always the right fix: can get you a This is a common use case so not sure |
https://channels.readthedocs.io/en/stable/topics/databases.html#database-sync-to-async is recommended way to wrap. What changes do you expect in django-constance? |
@sergei-iurchenko Hi! Thanks for answering. Considering that the Constance API is as simple as Django's Settings API, It would've been better if the library handled the synchronous context to reduce the confusion for a beginner user. Doing this would also prevent the code from being coupled to the storage backend (Because you won't need to use |
I see your point. |
Describe the problem
When used in an async view, you need to access the object with a
sync_to_async
wrapper or this error will get raised:SynchronousOnlyOperation: You cannot call this from an async context - use a thread or sync_to_async
Steps to reproduce
Create an async view in Django 3.1 and try to access an attribute of the
config
object.System configuration
The text was updated successfully, but these errors were encountered: