Python Twisted 和数据库连接
我们的工作项目包括同步应用程序(短期)和异步 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
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?
在 Twisted 中,您基本上需要一个函数的包装器,该函数返回 Deferred(例如 Twisted DB 层)、等待其结果并返回它们。但是,您不能忙等待,因为这会耗尽您的反应器周期,并且使用 Twisted 非阻塞等待检查任务是否完成可能效率很低。
inlineCallbacks 或 deferredGenerator 能解决您的问题吗?他们需要现代的 Twisted。 请参阅twistedmatrix 文档。
另一种选择是使用两种执行相同 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.
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.
您是否考虑过借用延续传递风格中的页面? 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.
我见过的所有数据库似乎都是顽固地同步的。
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.