简体   繁体   English

Python Twisted和数据库连接

[英]Python Twisted and database connections

Our projects at work include synchronous applications (short lived) and asynchronous Twisted applications (long lived). 我们的工作项目包括同步应用程序(短期)和异步Twisted应用程序(长期使用)。 We're re-factoring our database and are going to build an API module to decouple all of the SQL in that module. 我们正在重新分解我们的数据库,并将构建一个API模块来解耦该模块中的所有SQL。 I'd like to create that API so both synchronous and asynchronous applications can use it. 我想创建该API,以便同步和异步应用程序都可以使用它。 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. 对于同步应用程序,我想调用数据库API只返回数据(阻塞),就像使用MySQLdb一样,但对于异步应用程序,我想调用相同的API函数/方法是非阻塞的,可能返回延期。 Anyone have any hints, suggestions or help they might offer me to do this? 任何人都有任何提示,建议或帮助他们可能会让我这样做? Thanks in advance, Doug 道歉,先谢谢

twisted.enterprise.adbapi似乎要走了 - 你认为它不符合你的要求,如果是这样,你能解释一下原因吗?

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. 在Twisted中,你基本上想要一个函数的包装器,它返回一个Deferred(例如Twisted DB层),等待它的结果,然后返回它们。 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. 但是,您不能忙等待,因为这会耗尽您的反应堆周期,并且使用Twisted非阻塞等待检查任务完成可能效率低下。

Will inlineCallbacks or deferredGenerator solve your problem? inlineCallbacks或deferredGenerator会解决您的问题吗? They require a modern Twisted. 他们需要现代的Twisted。 See the twistedmatrix docs . 请参阅twistedmatrix文档

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. 另一个选择是有两个执行相同SQL模板的方法,一个使用runInteraction ,哪个阻塞,一个使用runQuery,它返回一个Deferred,但这将涉及更多的代码路径,它们做同样的事情。

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. Stackless Python 直接支持continuation ,如果你正在使用它,这种方法似乎已经获得了一些兴趣

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. 看来Twisted.enterprise.abapi通过使用线程来管理连接池和包装底层数据库库来解决这个问题。 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. 理想情况下,有一些方法可以将sqlalchemy和twisted整合在一起。 I found this project, nadbapi , which claims to do it, but it looks like it hasn't been updated since 2007. 我找到了这个项目, nadbapi ,声称这样做,但看起来它自2007年以来一直没有更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM