至尊宝的传说

Python 3.4 为异步IO 模块引入了新的临时API。asyncio这种方法类似于twisted@Bryan Ward的基于答案的答案 - 定义一个协议,一旦数据准备就调用它的方法:#!/usr/bin/env python3import asyncioimport osclass SubprocessProtocol(asyncio.SubprocessProtocol):

def pipe_data_received(self, fd, data):

if fd == 1: # got stdout data (bytes)

print(data)

def connection_lost(self, exc):

loop.stop() # end loop.run_forever()if os.name == 'nt':

loop = asyncio.ProactorEventLoop() # for subprocess' pipes on Windows

asyncio.set_event_loop(loop)else:

loop = asyncio.get_event_loop()try:

loop.run_until_complete(loop.subprocess_exec(SubprocessProtocol,

"myprogram.exe", "arg1", "arg2"))

loop.run_forever()finally:

loop.close()请参阅文档中的“子流程”。有一个高级接口asyncio.create_subprocess_exec()返回允许使用coroutine异步读取行的Process对象 (使用/ Python 3.5+语法):StreamReader.readline()asyncawait#!/usr/bin/env python3.5import asyncioimport localeimport sysfrom asyncio.subprocess import PIPEfrom contextlib import closing

async def readline_and_kill(*args):

# start child process

process = await asyncio.create_subprocess_exec(*args, stdout=PIPE)

# read line (sequence of bytes ending with b'\n') asynchronously

async for line in process.stdout:

print("got line:", line.decode(locale.getpreferredencoding(False)))

break

process.kill()

return await process.wait() # wait for the child process to exitif sys.platform == "win32":

loop = asyncio.ProactorEventLoop()

asyncio.set_event_loop(loop)else:

loop = asyncio.get_event_loop()with closing(loop):

sys.exit(loop.run_until_complete(readline_and_kill(

"myprogram.exe", "arg1", "arg2")))readline_and_kill() 执行以下任务:启动子进程,将其stdout重定向到管道从子进程'stdout异步读取一行杀死子进程等它退出如有必要,每个步骤都可以通过超时秒限制。

python subprocess 非阻塞_python中subprocess.PIPE上的非阻塞读取相关推荐

  1. python规则网格插值_Python中规则网格上的插值

    什么是合理的解决方案很大程度上取决于你试图用插值像素回答的问题--请注意清空器:对丢失的数据进行外推会导致非常误导的答案! 径向基函数插值/核平滑 就Python中可用的实际解决方案而言,填充这些像素 ...

  2. python 等高线地图 处理_python中不规则网格上地图覆盖等高线

    这是我的数据:Lon Lat Z Z2 pos 32.6 -13.6 41 9 CHIP 27.1 -16.9 43 12 CHOM 32.7 -10.2 46 14 ISOK 24.2 -13.6 ...

  3. python算法和数据结构_Python中的数据结构和算法

    python算法和数据结构 To 至 Leonardo da Vinci 达芬奇(Leonardo da Vinci) 介绍 (Introduction) The purpose of this ar ...

  4. python echo(msg) 字符串_python的subprocess模块

    1 os与commands模块 2 subprocess模块 3 subprocess.Popen类 我们几乎可以在任何操作系统上通过命令行指令与操作系统进行交互,比如Linux平台下的shell. ...

  5. python获取maco句柄_python之subprocess模块

    #!/usr/bin/env python#-*- coding: utf-8 -*- '''subprocess:提供执行系统相关命令的功能''' importsubprocess#call 执行命 ...

  6. pythonsubprocess执行多条shell命令_python中subprocess批量执行linux命令

    可以执行shell命令的相关模块和函数有: os.system os.spawn os.popen --废弃 popen --废弃 commands --废弃,3.x中被移除 以上执行shell命令的 ...

  7. python阻塞和非阻塞_Python基础必备知识:同步异步阻塞非阻塞

    一.状态介绍 在了解其他概念之前,我们首先要了解进程的几个状态.在程序运行的过程中,由于被操作系统的调度算法控制,程序会进入几个状态:就绪,运行和阻塞. 就绪(Ready)状态:当进程已分配到除CPU ...

  8. python异步查询数据库_Python的Tornado框架实现异步非阻塞访问数据库的示例

    tornado即是一个http非阻塞服务器, 就要用起来, 我们将用到tornado框架 ,mongodb数据库 以及motor(mongodb的异步驱动).来简单实现tornado的非阻塞功能. 其 ...

  9. python3 asyncio 不阻塞_Python中的并发处理之asyncio包使用的详解

    导语:本文章记录了本人在学习Python基础之控制流程篇的重点知识及个人心得,打算入门Python的朋友们可以来一起学习并交流. 本文重点: 1.了解asyncio包的功能和使用方法: 2.了解如何避 ...

  10. python决策树 多分类_Python中的决策树分类:您需要了解的一切

    python决策树 多分类 什么是决策树? (What is Decision Tree?) A decision tree is a decision support tool that uses ...

最新文章

  1. 从大数据到安全大数据分析
  2. 前 K 个高频元素—leetcode347
  3. Netty学习笔记(六)Pipeline的传播机制
  4. Python-条件控制及循环
  5. IDEA新特性:提前知道代码怎么走!
  6. 系统架构师成长之路(一)
  7. Kotlin入门(17)等式判断的情况
  8. 如何通过NSLOOKUP 命令查看MX 记录
  9. mybatis中concat的用法
  10. RuntimeError: The Session graph is empty. Add operations to the graph before calling run().
  11. 怎么做微信答题小程序
  12. 数据结构试卷及答案(十)
  13. 【RemoteJoy】PSP图像采集方案
  14. vue3实现商城左右联动数据---BScroll(vue3代码复制就能用)
  15. xtu oj 1078
  16. 做IT就要做到高层!
  17. Android智能硬件开发心得总结(二)
  18. C#微信开放平台开发——1、序言
  19. 洛谷P4196 半平面交
  20. Vue——props配置

热门文章

  1. 开源的物联网技术平台thingsboard安装测试及使用步骤
  2. react-native修改android包名-上传应用市场
  3. linux top 参数详解
  4. mysql中多个left join子查询写法以及别名用法
  5. 打造物联网+WiFi融合新模式 华三通信发布卫星AP
  6. [UI] 精美UI界面欣赏[11]
  7. LVS 实现负载均衡
  8. ButterKnife 8.6.0 使用
  9. C# winform窗体实现图片轮播
  10. linux落字效果代码,Linux 用echo输出带特效的字体