#!/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多进程完成模拟支付的更多相关文章

  1. Python实现支付宝在线支付

    windows系统安装Python虚拟环境 首先保证你的系统已经安装好了Python 安装virtualenv C:\WINDOWS\system32>pip3 install virtuale ...

  2. python多进程与多线程编程

    进程(process)和线程(thread)是非常抽象的概念.多线程与多进程编程对于代码的并发执行,提升代码运行效率和缩短运行时间至关重要.下面介绍一下python的multiprocess和thre ...

  3. Python多进程与多线程编程及GIL详解

    介绍如何使用python的multiprocess和threading模块进行多线程和多进程编程. Python的多进程编程与multiprocess模块 python的多进程编程主要依靠multip ...

  4. Python多进程库multiprocessing创建进程以及进程池Pool类的使用

    问题起因最近要将一个文本分割成好几个topic,每个topic设计一个regressor,各regressor是相互独立的,最后汇总所有topic的regressor得到总得预测结果.没错!类似bag ...

  5. Python多进程编程

    转自:Python多进程编程 阅读目录 1. Process 2. Lock 3. Semaphore 4. Event 5. Queue 6. Pipe 7. Pool 序. multiproces ...

  6. Python多进程(1)——subprocess与Popen()

    Python多进程方面涉及的模块主要包括: subprocess:可以在当前程序中执行其他程序或命令: mmap:提供一种基于内存的进程间通信机制: multiprocessing:提供支持多处理器技 ...

  7. Python多进程使用

    [Python之旅]第六篇(六):Python多进程使用   香飘叶子 2016-05-10 10:57:50 浏览190 评论0 python 多进程 多进程通信 摘要:   关于进程与线程的对比, ...

  8. Python使用mechanize模拟浏览器

    Python使用mechanize模拟浏览器 之前我使用自带的urllib2模拟浏览器去进行訪问网页等操作,非常多站点都会出错误,还会返回乱码.之后使用了 mechanize模拟浏览器,这些情况都没出 ...

  9. python多进程断点续传分片下载器

    python多进程断点续传分片下载器 标签:python 下载器 多进程 因为爬虫要用到下载器,但是直接用urllib下载很慢,所以找了很久终于找到一个让我欣喜的下载器.他能够断点续传分片下载,极大提 ...

  10. Python多进程multiprocessing使用示例

    mutilprocess简介 像线程一样管理进程,这个是mutilprocess的核心,他与threading很是相像,对多核CPU的利用率会比threading好的多. import multipr ...

随机推荐

  1. 《Bitcoin: A Peer-to-Peer Electronic Cash System》 中本聪写的比特币白皮书

    网址: https://bitcoin.org/bitcoin.pdf =============================================================

  2. error while loading shared libraries: libxml2.so.2: cannot open shared object file 解决方法

    参考: https://blog.csdn.net/qq_39779233/article/details/128215517 ==================================== ...

  3. mujoco_py.cymj.GlfwError: Failed to initialize GLFW

    安装mujoco后运行可视化界面代码报错: mujoco_py.cymj.GlfwError: Failed to initialize GLFW 解决方法: sudo apt-get install ...

  4. 【转载】 【Java分享客栈】我曾经的两个Java老师一个找不到工作了一个被迫转行了

      本文作者: 福隆苑居士 本文链接: https://www.cnblogs.com/fulongyuanjushi/p/16182465.html 版权声明: 本博客所有文章除特别声明外,均采用 ...

  5. mendeley存储是有限的吗

    用了mendeley好多年,总是有一个疑问,那就是这东西的云存储空间是不是有限的,如果是有限的话那么上限是多少?这东西不论是app还是桌面端都没有这个空间信息的大小,但是这东西必然是有上限的,那么这个 ...

  6. SMU Autumn 2023 Round 3(Div.1)

    SMU Autumn 2023 Round 3(Div.1) A. Find The Array 要满足"b数组内任意一个元素满足可以被数组两边的元素整除"这个条件,我们很容易想到 ...

  7. springboot2集成oauth2坑二(The bean 'scopedTarget.oauth2ClientContext', defined in class path resource )

    码云地址:https://gitee.com/lpxs/lp-springcloud.git 有问题可以多沟通:136358344@qq.com. 由于使用Enableoauth2sso注解一直报错, ...

  8. Linux C基础——” Makefile “ 文件管理大师你拜访过嘛?

    Make简介: 工程管理器,顾名思义,是指管理较多的文件 Make工程管理器也就是个"自动编译管理器",这里的"自动"是指它能够根据文件时间戳自动发现更新过的文 ...

  9. redux开发中的一个小坑

    redux的actiontypes,不可以重名,重名将会报错 When called with an action of type "inserttoken", the slice ...

  10. k8s pod挂载hostPath执行写时报错Permission denied

    关于hostPath的权限说明 最近项目中经常遇到pod中container挂载主机hostPath报错无权限问题: httpd@hostpath-volume:/test-volume$ touch ...