python多进程完成模拟支付
#!/usr/bin/python
# -*- coding: UTF-8 -*-
'''@auther :mr.qin
@IDE:pycharm''' from tool.Common import *
from tool.request_data_change_to_StrEncode import request_data_change_to_str
from tool.tencent_cloud_mysql_connect import Mysql_operation
import time
import requests
from multiprocessing import Process,Queue,Pool,Manager,Lock
import os my_op = Mysql_operation()
get_data = '''select order_id,order_sn from order_info limit 100;'''
data = my_op.sql_operation(get_data)
data_to_str = request_data_change_to_str()
lock=Lock() def testDoWeChatNotify(q):
fwh_order_dict = {}
fwh_order_id_list, fwh_order_sn_list = [], []
if data!=():
for a in data:
fwh_order_id=a['order_id']
fwh_order_sn=a['order_sn']
fwh_order_id_list.append(fwh_order_id)
fwh_order_sn_list.append(fwh_order_sn)
fwh_order_dict['fwh_order_id']=fwh_order_id_list
fwh_order_dict['fwh_order_sn']=fwh_order_sn_list
# print("put %s to queue...." % (fwh_order_dict))
q.put(fwh_order_dict) return q.qsize()#返回队列数量 def asynchronousPay_process(q,order_id,order_sn):
if (data!=()):
url_wechat_success_huidiao=fwh_test_api+'/index/Order/doWeChatNotify'
data_wechat_success_huidiao=data_to_str.requestDataToStr_firefoxAndChrome_fwh('''order_sn:{}
order_id:{}
meth_id:4
timestamp:157129653969
sign:0687b01b300b9e300d3996a9d2173f1380973e5a'''.format(order_sn,order_id))
# print(data_wechat_success_huidiao)
request_wechat_success_huidiao=requests.post(url=url_wechat_success_huidiao,headers=headers_form_urlencoded,data=data_wechat_success_huidiao)
response_wechat_success_huidiao=request_wechat_success_huidiao.json()
if '订单状态错误,非待支付订单' in response_wechat_success_huidiao['msg']:
print(data_wechat_success_huidiao)
# print(request_wechat_success_huidiao.json())
else:
print('待支付订单为空') def run_multiprocess():
p = Pool(9)
q = Manager().Queue()
size = list(range(testDoWeChatNotify(q)))
info = q.get()
for i in range(len(info['fwh_order_id'])):
lock.acquire()#加互斥锁
# print('当前进程{},父进程id{}'.format(os.getpid(),os.getppid()))
p.apply_async(func=asynchronousPay_process,
args=(q, info['fwh_order_id'][i], info['fwh_order_sn'][i]))
lock.release()#解锁 p.close()
p.join() if __name__=="__main__":
start_time = time.time() # 计算程序开始时间
run_multiprocess()
# q=Manager().Queue()
# size = list(range(testDoWeChatNotify(q)))
# info = q.get()
# for i in range(len(info['fwh_order_id'])):
# asynchronousPay_process(q,info['fwh_order_id'][i], info['fwh_order_sn'][i])
print('程序耗时{:.2f}'.format(time.time() - start_time)) # 计算程序总耗时
思路:与多线程实现思路差不多,但是所用模块不同,多进程用multiprocessing模拟,多线程用therading模块
多进程队列使用multiprocessing.Queue()或者multiprocessing.Manager().Queue(),多线程使用queue.Queue(),apply_async方法是apply方法的非阻塞式(异步)版本,Queue引入确实提高了执行速度,亲测:为使用多进程前支付100个订单用了四十多秒,使用之后用了5.x秒,
这根电脑的性能会有一些关系!
问题:
1.在调试的过程中我想用一个封装一个类来完成,但是调试了很久,都有问题(队列无法公用的问题),忘大佬指教;
2.线程启动的数量更多(进程启动了8个,线程启动了100个),为啥会没有多进程的执行速度快?(同时使用join的情况下),这里修正一下,多线程执行速度会快一些,因为代码跟运行速度不一致所以造成了这种错觉
3.会出现重复请求的请求,但是只会有一次(我创建了八个进程,为什么只会重复一次)
python多进程完成模拟支付的更多相关文章
- Python实现支付宝在线支付
windows系统安装Python虚拟环境 首先保证你的系统已经安装好了Python 安装virtualenv C:\WINDOWS\system32>pip3 install virtuale ...
- python多进程与多线程编程
进程(process)和线程(thread)是非常抽象的概念.多线程与多进程编程对于代码的并发执行,提升代码运行效率和缩短运行时间至关重要.下面介绍一下python的multiprocess和thre ...
- Python多进程与多线程编程及GIL详解
介绍如何使用python的multiprocess和threading模块进行多线程和多进程编程. Python的多进程编程与multiprocess模块 python的多进程编程主要依靠multip ...
- Python多进程库multiprocessing创建进程以及进程池Pool类的使用
问题起因最近要将一个文本分割成好几个topic,每个topic设计一个regressor,各regressor是相互独立的,最后汇总所有topic的regressor得到总得预测结果.没错!类似bag ...
- Python多进程编程
转自:Python多进程编程 阅读目录 1. Process 2. Lock 3. Semaphore 4. Event 5. Queue 6. Pipe 7. Pool 序. multiproces ...
- Python多进程(1)——subprocess与Popen()
Python多进程方面涉及的模块主要包括: subprocess:可以在当前程序中执行其他程序或命令: mmap:提供一种基于内存的进程间通信机制: multiprocessing:提供支持多处理器技 ...
- Python多进程使用
[Python之旅]第六篇(六):Python多进程使用 香飘叶子 2016-05-10 10:57:50 浏览190 评论0 python 多进程 多进程通信 摘要: 关于进程与线程的对比, ...
- Python使用mechanize模拟浏览器
Python使用mechanize模拟浏览器 之前我使用自带的urllib2模拟浏览器去进行訪问网页等操作,非常多站点都会出错误,还会返回乱码.之后使用了 mechanize模拟浏览器,这些情况都没出 ...
- python多进程断点续传分片下载器
python多进程断点续传分片下载器 标签:python 下载器 多进程 因为爬虫要用到下载器,但是直接用urllib下载很慢,所以找了很久终于找到一个让我欣喜的下载器.他能够断点续传分片下载,极大提 ...
- Python多进程multiprocessing使用示例
mutilprocess简介 像线程一样管理进程,这个是mutilprocess的核心,他与threading很是相像,对多核CPU的利用率会比threading好的多. import multipr ...
随机推荐
- 中国特供版4090D已经开始发售
由于美国政府的限制,NVIDIA公司等美国公司不允许向中国出口4090显卡,但是为了绕过美国政府的限制NVIDIA公司推出了中国特供版的4090D显卡. 4090d显卡和4090显卡区别大吗?可以说其 ...
- 【转载】联合发布|面向眩晕诊疗的中文医疗对话大模型MedChat发布!
原文地址: https://mp.weixin.qq.com/s/XrddDDpDXHKBcEueH8YXcA ============================================ ...
- 【转载】 Ubuntu下使用VSCode的launch.json及tasks.json编写
版权声明:本文为CSDN博主「子木呀」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明.原文链接:https://blog.csdn.net/qq_41687938/a ...
- 神经网络之卷积篇:详解卷积步长(Strided convolutions)
详解卷积步长 卷积中的步幅是另一个构建卷积神经网络的基本操作,让向展示一个例子. 如果想用3×3的过滤器卷积这个7×7的图像,和之前不同的是,把步幅设置成了2.还和之前一样取左上方的3×3区域的元素的 ...
- 一次生产环境mysql迁移操作(一)数据归档
一次生产环境mysql迁移操作(一)数据归档 一次生产环境mysql迁移操作(二)mysql空间释放(碎片整理) 背景 在项目过程中我们经常要对数据库进行迁移.归档.拆分等等操作,现在描述下几种方案 ...
- keycloak~scope客户端模板的使用
scope为何物? scope在oauth2中表示授权的范围,另外也可以理解为,根据认证时scope的参数,在构建jwt时,返回更多的信息:比如在keycloak中,你的可选scope(optiona ...
- Ansible 知识
Ref: guru99.com/ansible-tutorial.html https://www.digitalocean.com/community/tutorials/how-to-use-an ...
- C# 开源教程带你轻松掌握数据结构与算法
前言 在项目开发过程中,理解数据结构和算法如同掌握盖房子的秘诀.算法不仅能帮助我们编写高效.优质的代码,还能解决项目中遇到的各种难题. 给大家推荐一个支持C#的开源免费.新手友好的数据结构与算法入门教 ...
- Angular 18+ 高级教程 – Component 组件 の Template Binding Syntax
前言 这篇介绍一些基本的 Angular 模板语法. 参考 Docs – Understanding binding Render.Event Listening and DOM Manipulati ...
- CSS & JS Effect – Show More
效果 show more 是很常被使用的效果, 因为空间总是不够的丫. 比起 scroll, show more 的体验通常会好一些, 尤其在手机, 它有更好的引导. 实现思路 1. 卡片需要一个 ...