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的更多相关文章

  1. Python标准库06 子进程 (subprocess包)

    这里的内容以Linux进程基础和Linux文本流为基础.subprocess包主要功能是执行外部的命令和程序.比如说,我需要使用wget下载文件.我在Python中调用wget程序.从这个意义上来说, ...

  2. python协程(yield、asyncio标准库、gevent第三方)、异步的实现

    引言 同步:不同程序单元为了完成某个任务,在执行过程中需靠某种通信方式以协调一致,称这些程序单元是同步执行的. 例如购物系统中更新商品库存,需要用"行锁"作为通信信号,让不同的更新 ...

  3. asyncio标准库7 Producer/consumer

    使用asyncio.Queue import asyncio import random async def produce(queue, n): for x in range(1, n + 1): ...

  4. asyncio标准库5 TCP echo client and server

    server import asyncio async def handle_echo(reader, writer): data = await reader.read(100) message = ...

  5. asyncio标准库4 asyncio performance

    性能包括2部分 每秒并发请求数(Number of concurrent requests per second) 每秒请求负载(Request latency in seconds: min/ave ...

  6. asyncio标准库3 HTTP client example

    import aiohttp import asyncio import async_timeout async def fetch(session, url): async with async_t ...

  7. asyncio标准库2 Hello Clock

    如何调度协程,并发运行 asyncio.gather方法可以聚合协程or future def gather(*coros_or_futures, loop=None, return_exceptio ...

  8. asyncio标准库1 Hello World

    利用asyncio的event loop,编写和调度协程 coroutine [,kəuru:'ti:n] n. 协程 Simple coroutine(调用1个协程) import asyncio ...

  9. Python标准库---子进程 (subprocess包)

    这里的内容以Linux进程基础和Linux文本流为基础.subprocess包主要功能是执行外部的命令和程序.比如说,我需要使用wget下载文件.我在Python中调用wget程序.从这个意义上来说, ...

随机推荐

  1. 【算法笔记】B1047 编程团体赛

    1047 编程团体赛 (20 分) 编程团体赛的规则为:每个参赛队由若干队员组成:所有队员独立比赛:参赛队的成绩为所有队员的成绩和:成绩最高的队获胜. 现给定所有队员的比赛成绩,请你编写程序找出冠军队 ...

  2. vue点击tab跳转页面,给点击的tab添加样式,且解决刷新以后点击的tab样式消失问题

    <ul class="nij"> <li v-for="item in nav" @click="selectNav(item.ti ...

  3. Spring property文件配置方法以及如何与工程分离

    1,Spring使用property文件作为配置源    工程中难免出现一些需要每次部署都需要配置的参数,如数据源连接参数等,测试环境跟实际运行环境是不一样的.    使用spring框架的话,这些参 ...

  4. 1.nginx安装和配置

    1.安装 1.1安装编译工具及库文件 yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel wget 1. ...

  5. MVC Control 接收post请求的json数据

    [HttpPost] public string QueryInvoice() { string stream; using (var sr = new StreamReader(Request.In ...

  6. webstorm预览时把浏览器地址localhost改成IP

    可以通过 File --> Setting,搜索 deployment 点击 + 号 然后输入一个名称,选择:Local or mounted folder,点击 OK 接下来选择你的本地项目路 ...

  7. 从指定Dictionary中移除指定值项

    void Removeltems(Dictionary<int, ltem> _dicltemMap, ltem _item) { List<ltem> keys=new Li ...

  8. 路由器中带宽设置(Bandwidth) 20MHZ/40MHZ

    原文是英文的,在这里:http://routerguide.net/setting-up-20-mhz-or-40-mhz-bandwidth-how-to-improve-wifi-network- ...

  9. 微博关系服务与Redis的故事

    http://www.infoq.com/cn/articles/weibo-relation-service-with-redis?utm_source=articles_about_Redis&a ...

  10. 第三章:ionic环境搭建之windows篇

    下面是在windows操作系统上面安装ionic的步骤,已经在Windows 10/ 7/ XP下面通过验证. 安装JDK 1.1 下载(http://www.oracle.com/technetwo ...