【发布时间】:2020-02-10 15:18:20
【问题描述】:
我有一个函数可以查询一个大表以对其进行索引...它创建一个名为“all_accounts”的服务器端游标。
def get_all_accounts(self):
cursor = self.get_cursor('all_accounts')
cursor.execute("SELECT * FROM account_summary LIMIT 20000;")
然后我一次处理这 2,000 个左右,以插入 NoSQL 解决方案:
def index_docs(self, cursor):
while True:
# consume result over a series of iterations
# with each iteration fetching 2000 records
record_count = cursor.rowcount
records = cursor.fetchmany(size=2000)
if not records:
break
for r in records:
# do stuff
我希望 index_docs 函数能够并行 x10 使用游标 fetchmany() 调用,因为我的瓶颈不是由目标系统引起的,而是由我的脚本的单线程性质引起的。我过去做过一些异步/工作人员的事情,但是 psycopg2 光标似乎可能是个问题。想法?
【问题讨论】:
标签: python postgresql