单进程的执行结果
如图:
测试代码:
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))