问题描述
这只是一个很长的问题:“sync_to_async 如何与阻塞 IO 和 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)()
可以传递给 asgiref 的 sync_to_async
的第三个 kwarg 是一个 executor
执行器是 concurrent.futures.ThreadPoolExecutor
的类型
根据文档gevent.threadpool.ThreadPoolExecutor
或多或少继承和包装 concurrent.futures.ThreadPoolExecutor
例如我想使用 werkzeug
dispatcherMiddleware
,并包装一个 Asgi 应用程序。
认为 FastAPI 安装在旧的单体 django Wsgi 应用程序内部(使用 eventlet / gevent / psycogreen / 猴子补丁)
这是我的尝试。
基本上,如何获得 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,**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,**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
相关:
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)