Django支持异步数据库引擎

发布于 2025-01-27 02:59:50 字数 238 浏览 4 评论 0原文

我找不到有关Django支持异步数据库引擎的信息。例如,对于PostgreSQL Django,仅支持Psycopg2库,该库完全同步并且不支持任何支持,因为SQLite Django仅支持同步的SQLite3库。因此,我在django中并不是很好,当然我可能会误会,但是如果django asgi不支持异步数据库引擎的django asgi感(我的意思是,那么所有异步代码都会同步)?

第二个问题是,有什么方法可以在Django中使用异步发动机?

I can't find information about django support for asynchronous database engines. For example for postgresql django supports only psycopg2 library, that is completely synchronous and nothing more is supported, for sqlite django supports only sqlite3 library that is synchronous as well. So I'm not well orientiered in django and of course I can be mistaken, but what's the sense of django asgi if it doesn't support asynchronous database engines(I mean, then all the asynchronous code becomes synchronous) ?

And the second question, is there any way to use asynchronous engines in django ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

作妖 2025-02-03 02:59:50

这是来自Django文档

*您是否正在寻找哪种数据库Async支持? *

# DJANGO_SETTINGS_MODULE=settings.py python -m asyncio
>>> import asyncio
>>> from asgiref.sync import sync_to_async
>>> from django.db import connection
>>> # In an async context so you cannot use the database directly:
>>> connection.cursor()

django.core.exceptions.SynchronousOnlyOperation: You cannot call this from
an async context - use a thread or sync_to_async.
>>> # Nor can you pass resolved connection attributes across threads:
>>> await sync_to_async(connection.cursor)()
...
django.db.utils.DatabaseError: DatabaseWrapper objects created in a thread
can only be used in that same thread. The object with alias 'default' was
created in thread id 4371465600 and this is thread id 6131478528.

https://docs.djangoproject.com/en/4.0/4.0/topics/topics/async/

This is from the Django Docs

*Are you looking for what kind of database async support? *

# DJANGO_SETTINGS_MODULE=settings.py python -m asyncio
>>> import asyncio
>>> from asgiref.sync import sync_to_async
>>> from django.db import connection
>>> # In an async context so you cannot use the database directly:
>>> connection.cursor()

django.core.exceptions.SynchronousOnlyOperation: You cannot call this from
an async context - use a thread or sync_to_async.
>>> # Nor can you pass resolved connection attributes across threads:
>>> await sync_to_async(connection.cursor)()
...
django.db.utils.DatabaseError: DatabaseWrapper objects created in a thread
can only be used in that same thread. The object with alias 'default' was
created in thread id 4371465600 and this is thread id 6131478528.

https://docs.djangoproject.com/en/4.0/topics/async/

萌梦深 2025-02-03 02:59:50

据我所知,在Django Orm中没有对异步的支持。因此,您应该考虑使用不同的框架或集成不同的ORM,例如Sqlalchemy,

这是他们在文档中所说的:
“我们仍在为Django的ORM和其他部分提供异步支持。您可以期望在将来的版本中看到此内容。目前,您可以使用Sync_to_async()适配器与Django的同步部分进行交互。您还可以集成的一系列异步的python库。”

对于您的第二个问题:配置Django以使用SQLalchemy

但我强烈建议您考虑切换开关框架如果您没有很多Django绑定的书面代码。要像 fastapi aiohttp

As far as I know there is no support for async in Django ORM. So you should consider using different framework or integrate different ORM like sqlalchemy

Here is what they say in the doc:
"We’re still working on async support for the ORM and other parts of Django. You can expect to see this in future releases. For now, you can use the sync_to_async() adapter to interact with the sync parts of Django. There is also a whole range of async-native Python libraries that you can integrate with."

For your second question: Configuring Django to use SQLAlchemy

But I would strongly recommend to consider switching framework if you don't have a lot django bonded written code. To something like FastAPI, or aiohttp

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文