Python Twisted 和数据库连接

发布于 2024-08-11 01:21:02 字数 235 浏览 3 评论 0原文

我们的工作项目包括同步应用程序(短期)和异步 Twisted 应用程序(长期)。我们正在重构我们的数据库,并将构建一个 API 模块来解耦该模块中的所有 SQL。我想创建该 API,以便同步和异步应用程序都可以使用它。对于同步应用程序,我希望调用数据库 API 只返回数据(阻塞),就像使用 MySQLdb 一样,但对于异步应用程序,我希望调用相同的 API 函数/方法是非阻塞的,可能会返回延期。有人可以向我提供任何提示、建议或帮助吗? 提前致谢, 道格

Our projects at work include synchronous applications (short lived) and asynchronous Twisted applications (long lived). We're re-factoring our database and are going to build an API module to decouple all of the SQL in that module. I'd like to create that API so both synchronous and asynchronous applications can use it. For the synchronous applications I'd like calls to the database API to just return data (blocking) just like using MySQLdb, but for the asynchronous applications I'd like calls to the same API functions/methods to be non-blocking, probably returning a deferred. Anyone have any hints, suggestions or help they might offer me to do this?
Thanks in advance,
Doug

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

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

发布评论

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

评论(4

执手闯天涯 2024-08-18 01:21:03

twisted.enterprise.adbapi 似乎是可行的方法 - 你是吗?认为它不符合您的要求,如果是的话,您能解释一下原因吗?

twisted.enterprise.adbapi seems the way to go -- do you think it fails to match your requirements, and if so, can you please explain why?

橘亓 2024-08-18 01:21:03

在 Twisted 中,您基本上需要一个函数的包装器,该函数返回 Deferred(例如 Twisted DB 层)、等待其结果并返回它们。但是,您不能忙等待,因为这会耗尽您的反应器周期,并且使用 Twisted 非阻塞等待检查任务是否完成可能效率很低。

inlineCallbacks 或 deferredGenerator 能解决您的问题吗?他们需要现代的 Twisted。 请参阅twistedmatrix 文档

def thingummy():
   thing = yield makeSomeRequestResultingInDeferred()
   print thing #the result! hoorj!
thingummy = inlineCallbacks(thingummy)

另一种选择是使用两种执行相同 SQL 模板的方法,其中一种使用 runInteraction,它会阻塞,还有一个使用 runQuery,它返回一个 Deferred,但这会涉及更多执行相同操作的代码路径。

Within Twisted, you basically want a wrapper around a function which returns a Deferred (such as the Twisted DB layer), waits for it's results, and returns them. However, you can't busy-wait, since that's using up your reactor cycles, and checking for a task to complete using the Twisted non-blocking wait is probably inefficient.

Will inlineCallbacks or deferredGenerator solve your problem? They require a modern Twisted. See the twistedmatrix docs.

def thingummy():
   thing = yield makeSomeRequestResultingInDeferred()
   print thing #the result! hoorj!
thingummy = inlineCallbacks(thingummy)

Another option would be to have two methods which execute the same SQL template, one which uses runInteraction, which blocks, and one which uses runQuery, which returns a Deferred, but that would involve more code paths which do the same thing.

≈。彩虹 2024-08-18 01:21:03

您是否考虑过借用延续传递风格中的页面? Stackless Python 直接支持延续,如果您正在使用它,并且该方法似乎有 < a href="http://www.google.com/search?q=twisted+stackless+python" rel="nofollow noreferrer">已经引起了一些兴趣。

Have you considered borrowing a page from continuation-passing style? Stackless Python supports continuations directly, if you're using it, and the approach appears to have gained some interest already.

独夜无伴 2024-08-18 01:21:03

我见过的所有数据库似乎都是顽固地同步的。

Twisted.enterprise.abapi 似乎通过使用线程来管理连接池并包装底层数据库库来解决这个问题。这显然不理想,但我想它会起作用,但我自己还没有实际尝试过。

理想情况下,会有某种方法将 sqlalchemy 和twisted 集成起来。我发现这个项目 nadbapi 声称可以做到这一点,但看起来它并没有自2007年以来就没有更新过。

All the database libraries I've seen seem to be stubbornly synchronous.

It appears that Twisted.enterprise.abapi solves this problem by using a threads to manage a connection pool and wrapping the underlying database libraries. This is obviously not ideal, but I suppose it would work, but I haven't actually tried it myself.

Ideally there would be some way to have sqlalchemy and twisted integrated. I found this project, nadbapi, which claims to do it, but it looks like it hasn't been updated since 2007.

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