明輝手游網(wǎng)中心:是一個免費提供流行視頻軟件教程、在線學(xué)習(xí)分享的學(xué)習(xí)平臺!

怎么完成python3完成并發(fā)訪問水平切分表

[摘要]本篇文章給大家?guī)淼膬?nèi)容是關(guān)于如何實現(xiàn)python3實現(xiàn)并發(fā)訪問水平切分表,有一定的參考價值,有需要的朋友可以參考一下,希望對你有所幫助。場景說明假設(shè)有一個mysql表被水平切分,分散到多個host...

本篇文章給大家?guī)淼膬?nèi)容是關(guān)于如何實現(xiàn)python3實現(xiàn)并發(fā)訪問水平切分表,有一定的參考價值,有需要的朋友可以參考一下,希望對你有所幫助。

場景說明

假設(shè)有一個mysql表被水平切分,分散到多個host中,每個host擁有n個切分表。
如果需要并發(fā)去訪問這些表,快速得到查詢結(jié)果, 應(yīng)該怎么做呢?
這里提供一種方案,利用python3的asyncio異步io庫及aiomysql異步庫去實現(xiàn)這個需求。

代碼演示

import logging
import random
import asynciofrom aiomysql 
import create_pool
# 假設(shè)mysql表分散在8個host, 每個host有16張子表
TBLES = {    "192.168.1.01": "table_000-015", 
# 000-015表示該ip下的表明從table_000一直連續(xù)到table_015
    "192.168.1.02": "table_016-031",  
      "192.168.1.03": "table_032-047",   
       "192.168.1.04": "table_048-063",  
         "192.168.1.05": "table_064-079",   
          "192.168.1.06": "table_080-095",  
            "192.168.1.07": "table_096-0111",  
              "192.168.1.08": "table_112-0127",
}
USER = "xxx"PASSWD = "xxxx"# wrapper函數(shù),用于捕捉異常def query_wrapper(func):
    async def wrapper(*args, **kwargs):
        try:
            await func(*args, **kwargs)        except Exception as e:
            print(e)    return wrapper
            # 實際的sql訪問處理函數(shù),通過aiomysql實現(xiàn)異步非阻塞請求@
            query_wrapperasync def query_do_something(ip, db, table):
    async with create_pool(host=ip, db=db, user=USER, password=PASSWD) as pool:
        async with pool.get() as conn:
            async with conn.cursor() as cur:
                sql = ("select xxx from {} where xxxx")
                await cur.execute(sql.format(table))
                res = await cur.fetchall()        
  # then do something...# 生成sql訪問隊列, 隊列的每個元素包含要對某個表進行訪問的函數(shù)及參數(shù)def gen_tasks():
    tasks = []    for ip, tbls in TBLES.items():
        cols = re.split('_ -', 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實現(xiàn)函數(shù)異步調(diào)用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()

以上就是如何實現(xiàn)python3實現(xiàn)并發(fā)訪問水平切分表的詳細內(nèi)容,更多請關(guān)注php中文網(wǎng)其它相關(guān)文章!


學(xué)習(xí)教程快速掌握從入門到精通的SQL知識。