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. ...
随机推荐
- python数据分析与可视化【思维导图】
python数据分析与可视化常用库 numpy+matplotlib+pandas 思维导图 图中难免有错误,后期随着学习与应用的深入,会不断修改更新. 当前版本号:1.0 numpy介绍 NumPy ...
- xcode运行sh权限问题
Showing Recent Messages Command /bin/sh emitted errors but did not return a nonzero exit code to ind ...
- .NET周报【1月第2期 2023-01-13】
国内文章 [ASP.NET Core]按用户等级授权 https://www.cnblogs.com/tcjiaan/p/17024363.html 本文介绍了ASP.NET Core如何按照用户等级 ...
- 网络安全之frp内网穿透
前言 内网穿透,也即 NAT 穿透,进行 NAT 穿透是为了使具有某一个特定源 IP 地址和源端口号的数据包不被 NAT 设备屏蔽而正确路由到内网主机 环境需求 一台公网服务器(linux操作系统) ...
- Java基础学习笔记-常量与变量♪(^∇^*)
常量与变量相同点 都有作用域,跟JS差不多, 变量的作用域:一对{ }之间有效 1.局部 2.全局 2.1.属于类的量(类常量和类变量) 2.2.属于实例的量(实例常量和实例变量) • 前面都可加权限 ...
- angular小练习--手写弹出窗口以及文件上传或者复制粘贴,后读取打印文件内容
实现代码如下 <page-header> <ng-template> </ng-template> </page-header> <div> ...
- .Net6 使用 Ocelot + Consul 看这篇就够了
前言 卯兔敲门,新的一年,祝大家前'兔'似锦!希望大家假后还能找到公司的大门 O(∩_∩)O !书接上文,我们使用了 Consul实现了服务注册与发现,对Consul不熟悉的同学可以先看看.这篇文章我 ...
- C# 线程同步查漏补缺
同步构造 当线程 A 在等待一个同步构造,另一个线程 B 持有构造一直不释放,那么就会导致线程 A 阻塞.同步构造有用户模式构造和内核模式构造. 用户模式构造通过 CPU 指令来协调线程,所以速度很快 ...
- 【踩坑记录】单测中@PostConstruct多次执行
问题复现: 单测中@PostConstruct修饰的方法被多次执行 原因: @PostConstruct在Spring中常用于在构造函数后初始化对象,执行顺序如下: 构造方法->成员变量注入-& ...
- 插入排序(CSP-J 2021 T2)我有新思路了,链接:https://www.cnblogs.com/wjk53233/p/16533752.html
我有新思路了,链接:https://www.cnblogs.com/wjk53233/p/16533752.html 我有新思路了,链接:https://www.cnblogs.com/wjk5323 ...