zoukankan      html  css  js  c++  java
  • python使用多进程提高运行效率

    单进程的执行结果

    如图:

     测试代码:

    from knight_util.util import KnightUtil
    import time
    m1 = KnightUtil().mysql_()
    
    
    def tk_(user_id):
        sql = '''SELECT token, client_code FROM sso.user_session WHERE user_id={} AND status='ENABLED';'''.format(user_id)
        m1.execute(sql)
        platform_dict = {}
        for data in m1.fetchall():
            platform_dict.update({data[1]: "{}".format(data[0])})
        print(platform_dict)
    
    
    if __name__ == '__main__':
        start_time = time.time()
        for i in range(100):
            tk_(2397)
        stop_time = time.time()
        print('代码运行时间为:{}'.format(stop_time - start_time))

    多进程的执行结果

    如图:

     测试代码:

    from knight_util.util import KnightUtil
    from multiprocessing import Pool
    import time
    m1 = KnightUtil().mysql_()
    
    
    def tk_(user_id):
        sql = '''SELECT token, client_code FROM sso.user_session WHERE user_id={} AND status='ENABLED';'''.format(user_id)
        m1.execute(sql)
        platform_dict = {}
        for data in m1.fetchall():
            platform_dict.update({data[1]: "{}".format(data[0])})
        print(platform_dict)
    
    
    if __name__ == '__main__':
        start_time = time.time()
        pool = Pool(10)
        for i in range(100):
            pool.apply_async(tk_, args=(2397,))
        pool.close()
        pool.join()
        stop_time = time.time()
        print('代码运行时间为:{}'.format(stop_time - start_time))

  • 相关阅读:
    clion打断点不生效
    PHP加密解密
    细说MySQL表操作
    细说MySQL数据库操作
    终端(命令行)连接MySQL
    MySQL结构
    求1!+(1!+3!)+(1!+3!+5!)+...+(1!+3!+5!+7!+9!)的值
    react 生命周期
    React TS 组件 Demo
    react-redux 实现原理
  • 原文地址:https://www.cnblogs.com/xiamaojjie/p/14240300.html
Copyright © 2011-2022 走看看