Python django async ORM sync_to_async executor在使用gevent时交换出concurrent.futures.ThreadPoolExecutor

Python django async ORM sync_to_async executor在使用gevent时交换出concurrent.futures.ThreadPoolExecutor,python,django,psycopg2,gevent,eventlet,Python,Django,Psycopg2,Gevent,Eventlet,这只是一个很长的问题:“sync_to_async如何与阻塞IO和gevent/psycogreen一起工作?” 例如: 从myapp.models导入SomeModel 从asgiref.sync导入同步到异步 从gevent.threadpool导入ThreadPoolExecutor作为GThreadPoolExecutor 形态={ “线程敏感”:False, “executor”:GThreadPoolExecutor(最大工作线程数=1) } 等待同步到异步(SomeModel.ob

这只是一个很长的问题:“sync_to_async如何与阻塞IO和gevent/psycogreen一起工作?”

例如:

从myapp.models导入SomeModel
从asgiref.sync导入同步到异步
从gevent.threadpool导入ThreadPoolExecutor作为GThreadPoolExecutor
形态={
“线程敏感”:False,
“executor”:GThreadPoolExecutor(最大工作线程数=1)
}
等待同步到异步(SomeModel.objects.all,**conf)()
可以传递给asgiref的
sync\u to_async
的第三个kwarg是
执行器
执行器是一种
并发.futures.ThreadPoolExecutor

根据文档
gevent.threadpool.ThreadPoolExecutor
或多或少地继承和包装并发.futures.ThreadPoolExecutor

例如,假设我想使用一个
werkzeug
dispatchermidware
,并包装一个ASGI应用程序

试想一下,FastAPI安装在旧的单片django WSGI应用程序内部(使用eventlet/gevent/psycogreen/monkey补丁)

这是我的尝试

基本上,如何获得django异步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
相关的: