python使用协程完成批量模拟支付

import asyncio
import sys
from queue import Queue


sys.path.append("../")
# from tool.__init__ import *
from tool.Common import *
from tool.tencent_cloud_mysql_connect import Mysql_operation
from tool.request_data_change_to_StrEncode import request_data_change_to_str
import time
import gevent
import asyncio
import aiohttp#requests异步版

class doWeChatNotify(object):
    def __init__(self):
        # super().__init__()
        self.limit_num=100 #查询记录条数
        # self.WeChatNotify_sql='''select  order_id,order_sn from fw_order where `status`=0
        #                         and course_id=1569 ORDER BY create_time desc limit %d ;'''%(self.limit_num)
        self.WeChatNotify_sql ='''select order_id,order_sn from order_info  limit 500;'''
        self.fwh_test_api=fwh_test_api
        self.my_op=Mysql_operation()
        self.data = self.my_op.sql_operation(self.WeChatNotify_sql)
        self.data_to_str=request_data_change_to_str()
        self.fwh_order_dict = {}
        self.que = Queue()



    def testDoWeChatNotify(self):
        DoWeChatNotify_file='../tokenFileAndtxtFiles'+'/'+"DoWeChatNotify_asynchronousPay.txt"
        with open(DoWeChatNotify_file,'a',encoding='utf=-8') as file:
            str_first="order_id\t"+"order_sn\t\n" #文件首行数据
            file.write(str_first)
        fwh_order_id_list, fwh_order_sn_list = [], []

        if  self.data!=():
            for a in self.data:
                fwh_order_id=a['order_id']
                fwh_order_sn=a['order_sn']
                self.fwh_order_dict[fwh_order_id]=fwh_order_sn

                with open(DoWeChatNotify_file,'a',encoding='utf-8') as file2:#文件写入
                    str_DoWeChatNotifyInfo=str(fwh_order_id)+'\t'+str(fwh_order_sn)+'\t\n'
                    file2.flush() #清除缓冲区
                    file2.write(str_DoWeChatNotifyInfo)
                self.que.put(self.fwh_order_dict)#将数据添加至队列
        return self.que.qsize()#返回队列数量


    async def asynchronousPay(self,order_id,order_sn):
        if  (self.data!=()):
            url_wechat_success_huidiao=self.fwh_test_api+'/index/Order/doWeChatNotify'
            data_wechat_success_huidiao = self.data_to_str.requestDataToStr_firefoxAndChrome_fwh('''order_sn:{}
            order_id:{}
            meth_id:4
            timestamp:157129653969
            sign:0687b01b300b9e300d3996a9d2173f1380973e5a'''.format(order_sn, order_id))
            async with aiohttp.ClientSession() as session:
                async with session.post(url=url_wechat_success_huidiao,
                                                           headers=headers_form_urlencoded,
                                                           data=data_wechat_success_huidiao) as response:
                    await asyncio.sleep(1)
                    return await response.read()

        else:
            print('待支付订单为空')


    async def run(self):
        self.testDoWeChatNotify()  # 获取队列数量
        order_id = asyncio.Semaphore(500)  # 限制并发量为500
        to_get = [self.asynchronousPay(order_id,order_sn) for order_id,order_sn in self.que.get().items()]  # 总共1000任务
        # threads = [gevent.spawn(self.asynchronousPay, order_id, order_sn) for order_id, order_sn in
        #            self.que.get().items()]
        # gevent.joinall(threads)#gevent协程写法
        await asyncio.wait(to_get)

    def run_startAll(self):
        loop = asyncio.get_event_loop()
        loop.run_until_complete(self.run())
        loop.close()


if __name__=="__main__":  
    start_time = time.time()  # 计算程序开始时间
    wechfy=doWeChatNotify()
    wechfy.run_startAll()
    print('程序耗时{:.2f}'.format(time.time() - start_time))  # 计算程序总耗时

总结:
  1.使用协程请求需用到aiohttp这个库(我把它理解成是requests的异步版本),如果直接用requests这个库去请求则就不能发挥协程的优势(涉及到io操作)或者直接说不能称为了真正的协程;
  2.
async/awiat关键字的使用:配合asyncio模块一起使用(结合aiohttp)
  3.执行500个订单的时间为11.x秒,跟多线程执行差不多(但是这个对系统的开销会小很多)
 
posted @ 2019-12-30 12:55  wan了个蛋  阅读(396)  评论(0编辑  收藏  举报