Well, one workaround that may serve if your dataset isn't too large is to use a cache (or dict) and pre-load it.
Unlike the db, caches tend to be more async friendly, unless of course your cache is in your db.... :-) This just needs to be pre-loaded on startup (<your app>.apps.:<your app AppConfig>.ready() is a good spot for this), and maybe add a signal on the relevant model to reload if it changes. HTH, David On Mon, Jun 21, 2021 at 5:22 PM Konstantin Kuchkov < [email protected]> wrote: > Thanks for the explanation! > > To get it working, I will replace my model converters with basic > converters and do corresponding lookups in the views. > > On Sunday, 20 June 2021 at 16:16:58 UTC-7 Andrew Godwin wrote: > >> Ah yes, that's because the URL parsing/conversion all happens in the main >> (async, in ASGI mode) thread - and because to_python is a a synchronous >> function interface, there is literally nothing you can do to make it work. >> >> The only real way of fixing this would be for Django to patch the URL >> converters and run them all in a synchronous mode unless they were >> decorated as "safe" somehow, much like we do for middleware. It's not a >> terribly hard patch to make, but it does mean you don't have an immediate >> solution. There are ways of trying to work around it, but they will all >> result in you blocking the async thread while the ORM query runs, which >> would be disastrous in production. >> >> My apologies for not getting this in for the original async view patch - >> I had forgotten converters ran in the URL resolver. >> >> Andrew >> >> On Sat, Jun 19, 2021, at 12:31 AM, [email protected] wrote: >> >> >> I'm trying to switch from WSGI to ASGI and I'm having issues with my >> model id to model converters. >> I define my converters like this >> def to_python(self, primary_key): >> try: >> return self.model.objects.get(pk=primary_key) >> except self.model.DoesNotExist: >> raise ValueError >> When I go to a url where a converter is used I get a >> SynchronousOnlyOperation. >> Seems like converters are executed in an async context. >> If I use sync_to_async, I get a coroutine in my view instead of a model >> instance. Awaiting it here would defeat the purpose of having a converter. >> >> Any ideas how to fix this? >> >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Django users" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to [email protected]. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/django-users/ef60ed71-7f4f-482d-b540-bdf89e249aa4n%40googlegroups.com >> <https://groups.google.com/d/msgid/django-users/ef60ed71-7f4f-482d-b540-bdf89e249aa4n%40googlegroups.com?utm_medium=email&utm_source=footer> >> . >> >> >> -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To view this discussion on the web visit > https://groups.google.com/d/msgid/django-users/c130ef88-4dbf-42b9-a7b3-f0ba0b174a0an%40googlegroups.com > <https://groups.google.com/d/msgid/django-users/c130ef88-4dbf-42b9-a7b3-f0ba0b174a0an%40googlegroups.com?utm_medium=email&utm_source=footer> > . > -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAE5VhgU1Z0RaVrshaANE75mGQ21nATJVVjpc5cnAQQcb%2BLG9rw%40mail.gmail.com.

