asyncio标准库6 Threads & Subprocess
Threads
import asyncio
def compute_pi(digits):
# implementation
return 3.14
async def main(loop):
digits = await loop.run_in_executor(None, compute_pi, 20000)
print("pi: %s" % digits)
loop = asyncio.get_event_loop()
loop.run_until_complete(main(loop))
loop.close()
# AbstractEventLoop.run_in_executor(executor, func, *args)
# Executor (pool of threads or pool of processes)
Subprocess
Run a subprocess and read its output
import asyncio
async def run_command(*args):
# Create subprocess
process = await asyncio.create_subprocess_exec(
*args,
# stdout must a pipe to be accessible as process.stdout
stdout=asyncio.subprocess.PIPE)
# Wait for the subprocess to finish
stdout, stderr = await process.communicate()
# Return stdout
return stdout.decode().strip()
loop = asyncio.get_event_loop()
# Gather uname and date commands
commands = asyncio.gather(run_command('uname'), run_command('date'))
# Run the commands
uname, date = loop.run_until_complete(commands)
# Print a report
print('uname: {}, date: {}'.format(uname, date))
loop.close()
Communicate with a subprocess using standard streams
import asyncio
async def echo(msg):
# Run an echo subprocess
process = await asyncio.create_subprocess_exec(
'cat',
# stdin must a pipe to be accessible as process.stdin
stdin=asyncio.subprocess.PIPE,
# stdout must a pipe to be accessible as process.stdout
stdout=asyncio.subprocess.PIPE)
# Write message
print('Writing {!r} ...'.format(msg))
process.stdin.write(msg.encode() + b'\n')
# Read reply
data = await process.stdout.readline()
reply = data.decode().strip()
print('Received {!r}'.format(reply))
# Stop the subprocess
process.terminate()
code = await process.wait()
print('Terminated with code {}'.format(code))
loop = asyncio.get_event_loop()
loop.run_until_complete(echo('hello!'))
loop.close()
asyncio标准库6 Threads & Subprocess的更多相关文章
- Python标准库06 子进程 (subprocess包)
这里的内容以Linux进程基础和Linux文本流为基础.subprocess包主要功能是执行外部的命令和程序.比如说,我需要使用wget下载文件.我在Python中调用wget程序.从这个意义上来说, ...
- python协程(yield、asyncio标准库、gevent第三方)、异步的实现
引言 同步:不同程序单元为了完成某个任务,在执行过程中需靠某种通信方式以协调一致,称这些程序单元是同步执行的. 例如购物系统中更新商品库存,需要用"行锁"作为通信信号,让不同的更新 ...
- asyncio标准库7 Producer/consumer
使用asyncio.Queue import asyncio import random async def produce(queue, n): for x in range(1, n + 1): ...
- asyncio标准库5 TCP echo client and server
server import asyncio async def handle_echo(reader, writer): data = await reader.read(100) message = ...
- asyncio标准库4 asyncio performance
性能包括2部分 每秒并发请求数(Number of concurrent requests per second) 每秒请求负载(Request latency in seconds: min/ave ...
- asyncio标准库3 HTTP client example
import aiohttp import asyncio import async_timeout async def fetch(session, url): async with async_t ...
- asyncio标准库2 Hello Clock
如何调度协程,并发运行 asyncio.gather方法可以聚合协程or future def gather(*coros_or_futures, loop=None, return_exceptio ...
- asyncio标准库1 Hello World
利用asyncio的event loop,编写和调度协程 coroutine [,kəuru:'ti:n] n. 协程 Simple coroutine(调用1个协程) import asyncio ...
- Python标准库---子进程 (subprocess包)
这里的内容以Linux进程基础和Linux文本流为基础.subprocess包主要功能是执行外部的命令和程序.比如说,我需要使用wget下载文件.我在Python中调用wget程序.从这个意义上来说, ...
随机推荐
- C++_类继承2-多态公有继承
有时候希望同一个方法在派生类和基类中的行为是不同的.换句话说,方法的行为取决于调用该方法的对象.这种较复杂的行为称为多态——具有多种形态.即同一种方法其行为随上下文而异.有两种重要的机制可用于实现多态 ...
- DP设状态 : 状压与线
[NOIP2017]宝藏(状压) [AHOI2009]中国象棋(状压) [BZOJ1814] URAL1519 Formula 1(插头\(DP\)模板) 新链接 : Luogu5056 , dark ...
- 一款不错的Linux终端颜色设置
PS1="\[\e[37;40m\][\[\e[32;40m\]\u\[\e[37;40m\]@\h \[\e[36;40m\]\w\[\e[0m\]]\\$ " #步骤# vi ...
- JS中||的某些用法
var a = 0 ||'sda';console.log(a);//sda var a = '' ||'sda';console.log(a);//sda
- Oracle的pipelined函数实现高性能大数据处理
从Oracle 8开始,我们就可以从一个collection类型的数据集合中查询出数据,这个集合称之为"虚拟表".它的方法是"SELECT FROM TABLE(CAST ...
- 可编辑的el-table表格,结合input输入,upload文件上传的表格
最近整理了一下,table表格的编辑状态,把一般表格里需要输入的类型都放进来了,实现的功能如图 这里面的input输入框没什么好说的,绑定对应的值就可以,要注意的是组件上传的upload,这个 ...
- layer弹出层显示在top顶层
父页面 导入 layer.js 或者 layui.all.js,导入后就能正常显示在父窗口页面区域. 1.显示在顶层窗口 top.layer.open({ type: 2, area: ['98%', ...
- GreenPlum 大数据平台--并行备份(四)
01,并行备份(gp_dump) 1) GP同时备份Master和所有活动的Segment实例 2) 备份消耗的时间与系统中实例的数量没有关系 3) 在Master主机上备份所有DDL文件和GP相关的 ...
- Oracle 单实例数据库安装和real application clusters数据库安装的区别
在想了解Oracle单实例数据可和RAC数据库前,请确保你已经知道了数据库和实例的关系,如果不了解,请参考Oracle 数据库实例和数据库. 单实例数据库模式 单实例模式下,一个数据库只能通过一个实例 ...
- Hadoop Ecosystem related ports
本文总结了Hadoop生态系统中各个组件使用的端口,包括了HDFS,Map Reduce,HBase,Hive,Spark,WebHCat,Impala,Alluxio,Sqoop等,后续会持续更新. ...