python 多进程和异步io的有机结合 Error in atexit._run_exitfuncs
import asyncio
import aiohttp
from concurrent.futures import ProcessPoolExecutor, as_completed ,ThreadPoolExecutor
import time async def post_http():
# 示例
url = ''
data = ''
async with aiohttp.ClientSession() as session:
async with session.post(url=url, data=data, headers={}, timeout=60) as resp:
r_json = await resp.json()
return r_json async def t_handler(data, t_flag, p_flag, semaphore):
async with semaphore:
for d in data:
print(f'pid:{p_flag} tid:{t_flag} data:{d}')
await asyncio.sleep(1) # 处理费时的io操作,比如httprequest
return def p_handler(datas, p_flag):
# 线程并发数需要有限制 linux打开文件最大默认为1024 win为509 待确认
ts = time.time()
num = 10 # 最大并发数
count = len(datas)
block = int(count / num) + 1
tar_datas = [datas[i * block: (i + 1) * block if (i + 1) * block < count else count] for i in range(num)]
semaphore = asyncio.Semaphore(num)
tasks = [t_handler(d, i, p_flag, semaphore) for i, d in enumerate(tar_datas)] loop = asyncio.get_event_loop() # 基于当前线程 ,故在多线程中无法使用 只能在多进程中使用
loop.run_until_complete(asyncio.wait(tasks))
loop.close() return f'\033[0;32mprocess {p_flag} :cost {time.time() - ts}\033[0m' if __name__ == '__main__':
ts = time.time()
datas = [i for i in range(1000)]
datas = [datas[i * 100:(i + 1) * 100] for i in range(10)] # 每个进程要处理的数据 # 启动异步io 主线程调用 event_loop 在当前线程下启动异步io 实现并发
# res = p_handler(datas,1)
# print(res) p_num = 10
block_len = 100 datas = [datas[i * 100:(i + 1) * 100] for i in range(p_num)] # 每个进程要处理的数据
# ProcessPoolExecutor 可能与运行环境有关 官方的 with as 会主动释放线程 导致主线程退出时找不到进程池内进程已经被释放 导致Error in atexit._run_exitfuncs异常
executor = ProcessPoolExecutor(p_num)
futures = [executor.submit(p_handler, d, p_flag) for p_flag, d in enumerate(datas)]
for f in as_completed(futures):
if f.done():
res = f.result()
print(res) print(f'Exit!! cost:{time.time() - ts}')
python 多进程和异步io的有机结合 Error in atexit._run_exitfuncs的更多相关文章
- [译]Python中的异步IO:一个完整的演练
原文:Async IO in Python: A Complete Walkthrough 原文作者: Brad Solomon 原文发布时间:2019年1月16日 翻译:Tacey Wong 翻译时 ...
- python之爬虫_并发(串行、多线程、多进程、异步IO)
并发 在编写爬虫时,性能的消耗主要在IO请求中,当单进程单线程模式下请求URL时必然会引起等待,从而使得请求整体变慢 import requests def fetch_async(url): res ...
- 使用Python实现多线程、多进程、异步IO的socket通信
多线程实现socket通信服务器端代码 import socket import threading class MyServer(object): def __init__(self): # 初始化 ...
- 【Python】【异步IO】
# [[异步IO]] # [协程] '''协程,又称微线程,纤程.英文名Coroutine. 协程的概念很早就提出来了,但直到最近几年才在某些语言(如Lua)中得到广泛应用. 子程序,或者称为函数,在 ...
- 爬虫之多线程 多进程 自定义异步IO框架
什么是进程? 进程是程序运行的实例,是系统进行资源分配和调度的一个独立单位,它包括独立的地址空间,资源以及1个或多个线程. 什么是线程? 线程可以看成是轻量级的进程,是CPU调度和分派的基本单位. 进 ...
- 多线程,多进程和异步IO
1.多线程网络IO请求: #!/usr/bin/python #coding:utf-8 from concurrent.futures import ThreadPoolExecutor impor ...
- python 并发编程 异步IO模型
异步IO(Asynchronous I/O) Linux下的asynchronous IO其实用得不多,从内核2.6版本才开始引入.先看一下它的流程: 用户进程发起read操作之后,立刻就可以开始去做 ...
- Python 并发总结,多线程,多进程,异步IO
1 测量函数运行时间 import time def profile(func): def wrapper(*args, **kwargs): import time start = time.tim ...
- Python 协程/异步IO/Select\Poll\Epoll异步IO与事件驱动
1 Gevent 协程 协程,又称微线程,纤程.英文名Coroutine.一句话说明什么是线程:协程是一种用户态的轻量级线程. 协程拥有自己的寄存器上下文和栈.协程调度切换时,将寄存器上下文和栈保存到 ...
- Python 事件驱动与异步IO
一.事件驱动编程是一种编程范式,这里程序的执行流由外部事件来决定.它的特点是包含一个事件循环,当外部事件发生时使用回调机制来出发相应的处理.另外两种常见的编程范式是(单线程)同步以及多线程编程. 1. ...
随机推荐
- 一问读懂Web3 架构
最近看了一些Web3.0的文章,总结了一些个人的理解: Web3.0 通过区块链基础设施管理用户数据,重构用户和互联网平台之间的关系和交互,重新定义了互联网应用的架构方式和交互模式. Web 1.0 ...
- 迁移学习(IIMT)——《Improve Unsupervised Domain Adaptation with Mixup Training》
论文信息 论文标题:Improve Unsupervised Domain Adaptation with Mixup Training论文作者:Shen Yan, Huan Song, Nanxia ...
- 《Effective C++》再次探索traits技法
首先介绍C++标准程序库中的五种迭代器,关于这个可以看我的另一个笔记:http://blog.csdn.net/m0_37316917/article/details/70053513. 对于这五种分 ...
- 【深入浅出Spring原理及实战】「源码调试分析」结合DataSourceRegister深入分析ImportBeanDefinitionRegistrar的源码运作流程
每日一句 人的一生中不可能会一帆风顺,总会遇到一些挫折,当你对生活失去了信心的时候,仔细的看一看.好好回想一下你所遇到的最美好的事情吧,那会让你感觉到生活的美好. 注入案例代码 如何通过实现Sprin ...
- 刷题笔记——1267.A+B Problem
题目 1267.A+B Problem 代码 while True: try: a,b=map(int,input().strip().split()) print(a+b) except: brea ...
- 02安装一个最小化的Hadoop
安装一个最小化的Hadoop 为了学习HDFS和之后的MapReduce,我们需要安装一个Hadoop. Hadoop一共有3种运行模式 独立模式:不启动守护进程,所有程序运行在一个JVM进程中.独立 ...
- Java基础学习笔记-Java数据类型转换-(~ ̄▽ ̄)~
JAVA数据类型和JS数据类型转换不一样,因为它是强类型语言嘛 类型转换规则 不允许数值类型和布尔类型 转换 整型.实型.字符型数据可以混合运算 类型转换分类 自动类型转换-隐式转换 1.整数转换为小 ...
- Quartz.Net源码Example之Quartz.Examples.AspNetCore
Quartz.Examples.AspNetCore .NetCore的Web系统,后台主要执行多个触发器任务,前台展示所有触发器信息和正在执行的作业的相关信息,还可以通过访问health-UI来 ...
- Lodop打印小票
使用Lodop打印小票 1.html页面 <head> <meta http-equiv="Content-Type" content="text/ht ...
- Spring02---IOC-Debug查看Bean的实例化过程
1 简介 springIOC它是对bean进行管理. 我们通常可以通过xml.properties.yml.注解等来配置bean的信息 spring读取这些配置信息,解析,生成BeanDefiniti ...