-', tbls)
tblpre = "_".join(cols[:-2])
min_num = int(cols[-2])
max_num = int(cols[-1])
for num in range(min_num, max_num+1):
tasks.append(
(query_do_something, ip, 'your_dbname', '{}_{}'.format(tblpre, num))
)
random.shuffle(tasks)
return tasks# 按批量运行sql访问请求队列def run_tasks(tasks, batch_len):
try:
for idx in range(0, len(tasks), batch_len):
batch_tasks = tasks[idx:idx+batch_len]
logging.info("current batch, start_idx:%s len:%s" % (idx, len(batch_tasks)))
for i in range(0, len(batch_tasks)):
l = batch_tasks[i]
batch_tasks[i] = asyncio.ensure_future(
l[0](*l[1:])
)
loop.run_until_complete(asyncio.gather(*batch_tasks))
except Exception as e:
logging.warn(e)# main方法, 通过asyncio实现函数异步调用def main():
loop = asyncio.get_event_loop()
tasks = gen_tasks()
batch_len = len(TBLES.keys()) * 5 # all up to you
run_tasks(tasks, batch_len)
loop.close()
以上就是如何实现python3实现并发访问水平切分表的详细内容,更多请关注php中文网其它相关文章!
学习教程快速掌握从入门到精通的SQL知识。
……