ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2019-12-30 13:01:25  阅读:270  来源: 互联网

标签:fwh 批量 携程 python self order sn data id


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秒,跟多线程执行差不多(但是这个对系统的开销会小很多)
 

标签:fwh,批量,携程,python,self,order,sn,data,id
来源: https://www.cnblogs.com/qtclm/p/12119178.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有