并发编程 六 - 支付接口并发
2021/6/19 9:26:51
本文主要是介绍并发编程 六 - 支付接口并发,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
支付接口并发
需求:对支付接口做并发,验证账户金额的扣款(-)冻结(+),然后把执行结果写到一个日志文件
# @Time : '2021-6-19 07:58' # @Author : 'pc.kang' import time,json,requests from threading import Thread,Lock payid_list = {11111, 22222, 33333, 44444, 55555, 66666, 77777, 88888, 99999, 00000} url = "http://xxx.com/api/Pay.do" headers = { "Connection": "keep-alive", "Content-Length": "23", "Accept": "application/json, text/plain, */*", "X-Requested-With": "XMLHttpRequest", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36", "Content-Type": "application/json", "Origin": "http://xxx.com", "Referer": "http://xxx/api/", "Accept-Encoding": "gzip, deflate", "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8", "Cookie": "11379a8f66bf4fea9c2b8620642e33b0" } def run(lock): for num in payid_list: param = {"id": num} body = json.dumps(param) lock.acquire() res = requests.post(url=url, data=body, headers=headers) print(res.text) result = json.loads(str(res.text)) # if(result["code"] == "0"): # print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + " 付款单id: %s 付款成功 \n" % num) # else: # print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + " 付款单id: %s 付款失败,错误信息:%s \n" %(num,res.text)) if(result["code"] == "0"): with open("log.txt", "a", encoding="utf8") as fp: fp.write(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + " 付款单id: %s 付款成功 \n" %num) else: with open("log.txt", "a", encoding="utf8") as fp: fp.write(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + " 付款单id: %s 付款失败,错误信息:%s \n" %(num,res.text)) lock.release() if __name__=="__main__": lock = Lock() t_list = [Thread(target=run,args=(lock,))] print(t_list) for t in t_list: t.start() for t in t_list: t.join()
这篇关于并发编程 六 - 支付接口并发的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-07-02springboot项目无法注册到nacos-icode9专业技术文章分享
- 2024-06-26结对编程到底难不难?答案在这里
- 2024-06-19《2023版Java工程师》课程升级公告
- 2024-06-15matplotlib作图不显示3D图,怎么办?
- 2024-06-1503-Loki 日志监控
- 2024-06-1504-让LLM理解知识 -Prompt
- 2024-06-05做软件测试需要懂代码吗?
- 2024-06-0514-ShardingSphere的分布式主键实现
- 2024-06-03为什么以及如何要进行架构设计权衡?
- 2024-05-31全网首发第二弹!软考2024年5月《软件设计师》真题+解析+答案!(11-20题)