见 the docs:threadsafety是,我引用,
Currently 2, which means that threads
may share the module and connections,
but not cursors.
所以你的“游标池”构造(其中一个游标可能由不同的线程使用)似乎超出了线程安全级别.这不是共享连接的问题(因为你已经在连接的构造函数中正确地传递了线程,所以没关系)但是游标.您可能希望在线程第一次使用它之后将每个游标存储在threading.local中,这样每个线程都可以拥有自己的1-cursor“池”(不是键优化,但是:使新游标不是重型作业).
在你的问题2中,finally子句在生成器对象(通过调用生成器函数Get构建)全部完成时执行 – 要么是因为它提升了StopIteration,要么是因为它被垃圾收集(通常是因为它的最后引用有刚走了例如,如果呼叫者是:
def imthecaller():
for i, row in enumerate(Get()):
print i, row
if i > 1: break
# this is the moment the generators' finally-clause runs
print 'bye'
最后执行(最多)3行已经产生.