django async ORM исполнитель sync_to_async заменяет concurrent.futures.ThreadPoolExecutor при использовании gevent

Это просто длинный способ спросить: как sync_to_async работает с блокировкой ввода-вывода и gevent / psycogreen?

Например:

from myapp.models import SomeModel from asgiref.sync import sync_to_async from gevent.threadpool import ThreadPoolExecutor as GThreadPoolExecutor conf = { «thread_sensitive»: False, «executor»: GThreadPoolExecutor(max_workers=1) } await sync_to_async(SomeModel.objects.all, **conf)()

Третий kwarg, который может быть передан sync_to_async asgiref, — это executor исполнитель — это тип concurrent.futures.ThreadPoolExecutor

Согласно документации gevent.threadpool.ThreadPoolExecutor более или менее наследует и обертывает concurrent.futures.ThreadPoolExecutor

Скажем, например, я хочу использовать werkzeug DispatcherMiddleware и обернуть приложение ASGI.

Подумайте, что FastAPI установлен внутри более старого монолитного приложения django WSGI (с использованием патчей eventlet / gevent / psycogreen / monkey)

Вот моя попытка сделать это.

  • https://github.com/rednaks/django-async-orm/pull/7/files

В принципе, как получить django async-ish ORM?

try: from gevent.threadpool import ThreadPoolExecutor as GThreadPoolExecutor from django.conf import settings if settings.GEVENT_DJANGO_ASYNC_ORM: from gevent import monkey monkey.patch_all() def monkey_patch_the_monkey_patchers(ex): from .patch_gevent import _FutureProxy def submit(ex, fn, *args, **kwargs): # pylint:disable=arguments-differ print(fn, *args, **kwargs) with ex._shutdown_lock: # pylint:disable=not-context-manager if ex._shutdown: raise RuntimeError(‘cannot schedule new futures after shutdown’) future = ex._threadpool.spawn(fn, *args, **kwargs) proxy_future = _FutureProxy(future) proxy_future.__class__ = concurrent.futures.Future return proxy_future ex.submit = submit return ex MonkeyPoolExecutor = monkey_patch_the_monkey_patchers(GThreadPoolExecutor) conf = {«thread_sensitive»: False, «executor»: MonkeyPoolExecutor(max_workers=1)} executor_ = MonkeyPoolExecutor except Exception as e: print(e) print(‘defaulting django_async_orm’) pass

  • https://github.com/rednaks/django-async-orm/discussions/9
  • https://github.com/abersheeran/a2wsgi

связанные с:

  • async orm django 3.0
  • с concurrent.futures.ThreadPoolExecutor () в качестве исполнителя: … не ждет

Источник: ledsshop.ru

Стиль жизни - Здоровье!