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. [CSU1911]Card Game(FWT)

    [vjudge-CSU1911] FWT_or #include<cstring> #include<iostream> #include<algorithm> # ...

  2. ui-grid使用详解

    HTML <pre name="code" class="html"><!--ui-grid css--> <link rel=& ...

  3. 【算法笔记】B1030 完美数列(三种方法)

    1030 完美数列 (25 分) 给定一个正整数数列,和正整数 p,设这个数列中的最大值是 M,最小值是 m,如果 M≤mp,则称这个数列是完美数列. 现在给定参数 p 和一些正整数,请你从中选择尽可 ...

  4. CNN 卷积神经网络结构

    cnn每一层会输出多个feature map, 每个Feature Map通过一种卷积滤波器提取输入的一种特征,每个feature map由多个神经元组成,假如某个feature map的shape是 ...

  5. bzoj 1085骑士精神 迭代深搜

    题目传送门 题目大意:给出一幅棋盘,问能否复原,中文题面,不做解释. 思路:第一次写迭代深搜的题目,这道题还是挺经典的.这道题的状态很明显的每多搜一层就是多八倍,非常的多,而且又是t组输入,所以必定有 ...

  6. AtCoder Beginner Contest 113 A

    A - Discount Fare Time limit : 2sec / Memory limit : 1024MB Score: 100 points Problem Statement Ther ...

  7. Apache Shiro(五)-登录认证和权限管理ssm

    创建一个web动态项目 jar包 web.xml web.xml做了如下几件事情1. 指定spring的配置文件有两个 applicationContext.xml: 用于链接数据库的 applica ...

  8. follow Up — 20181101

    406. Minimum Size Subarray Sum public class Solution { /** * @param nums: an array of integers * @pa ...

  9. html自定义垂直导航菜单(加强版--自定义传入menu参数,支持JSONArray、JSArray、JSONObject、JSObject)

    在上一篇中我简单写了个html自定义垂直导航菜单,缺点很明显,里面的数据是固定死的,不能动态更改数据. 这里我重写了一个修改版的垂直二级导航菜单,将原先的menuBox.init(config);修改 ...

  10. Win32 进程间通信的分析与比较

    1 进程与进程通信 进程是装入内存并准备执行的程序,每个进程都有私有的虚拟地址空间,由代码.数据以及它可利用的系统资源(如文件.管道等)组成.多进程/多线 程是Windows操作系统的一个基本特征.M ...