我通过官方 sqlanydb 驱动程序将 Twisted 11 与 SQLAnywhere 12 一起使用。
一般来说,它工作得很好。
但偶尔应用程序会因第一个查询中止而崩溃。
如果一个查询有效,则后续所有查询也有效。然而我的测试很少能通过。
用它来开发是很糟糕的,而且 strace 也没有告诉我任何信息。有时它在 select() 中崩溃,有时在 mmap() 中崩溃......
我正在运行 64 位 Linux 并在本地运行 Sybase 作为 dbeng12 进行测试。
有人成功地使用这些组件吗?有什么建议如何解决这个问题吗?我之前在 Django 中使用 sqlanydb,它从未崩溃过。
使用prints,我发现它在DeferredList内部崩溃,重要的代码基本上如下:
class WhoisDb(object):
# ... shortened ...
def _get_contacts(self, dom):
if not dom:
self.d.errback(UnknownDomain(self._get_limit()))
return
self.dom = Domain._make(dom[0])
dl = defer.DeferredList( [
self.dbpool.runQuery(CON_SQL, (self.dom.dom_owner,)),
self.dbpool.runQuery(CON_SQL, (self.dom.dom_admin,)),
self.dbpool.runQuery(CON_SQL, (self.dom.dom_tech,)),
self.dbpool.runQuery(
LAST_UPDATE_SQL,
( self.dom.domName, )), ] ).addCallback(self._fmt_string)
def get_whois(self, domain):
self.d = defer.Deferred()
if not self._check_limit():
self.d.errback(LimitExceeded(MAX_PER_HOUR))
elif not RE_ALLOWED_TLDS.match(domain):
self.d.errback(UnknownDomain(self._get_limit()))
else:
self.dbpool.runQuery(
'select ' + DOM_FIELDS + ' from domains where '
'domain = ? or domain_idn = ?',
( domain, domain, )) \
.addCallback(self._get_contacts)
return self.d
_fmt_string() 如果崩溃则不会被调用。
在 gdb 内部,它是一个简单的 SIGSEV:
(gdb) run ~/.virtualenvs/whois/bin/trial test.test_protocol.ProtocolTestCase.test_correct_domain
Starting program: /home/hynek/.virtualenvs/whois/bin/python ~/.virtualenvs/whois/bin/trial test.test_protocol.ProtocolTestCase.test_correct_domain
[Thread debugging using libthread_db enabled]
test.test_protocol
ProtocolTestCase
test_correct_domain ... [New Thread 0x7ffff311a700 (LWP 6685)]
[New Thread 0x7ffff3099700 (LWP 6686)]
[New Thread 0x7ffff27dc700 (LWP 6723)]
[New Thread 0x7ffff1fdb700 (LWP 6724)]
[New Thread 0x7ffff17da700 (LWP 6725)]
[New Thread 0x7ffff0fd9700 (LWP 6729)]
Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x7ffff1fdb700 (LWP 6724)]
0x00007ffff4d4167c in ?? () from /opt/sqlanywhere12/lib64/libdbcapi_r.so
最佳答案
看来您的数据库不是线程安全的。为了使其成为稳定的连接,请执行以下操作:
self.dbpool = ConnectionPool(..., cp_min=1, cp_max=1)
这会将最大并发度设置为 1,并且 ThreadPool 将限制为 1 个线程,这意味着不会同时运行任何查询。这应该可以阻止您的非线程安全库给您带来任何麻烦,同时仍然在线程中运行查询并且不会阻塞主循环。
关于Python + Twisted + sqlanydb = abort(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5790435/