如何调度协程,并发运行

asyncio.gather方法可以聚合协程or future

def gather(*coros_or_futures, loop=None, return_exceptions=False)

import asyncio

async def print_every_second():
"Print seconds"
while True:
for i in range(60):
print(i, 's')
await asyncio.sleep(1) async def print_every_minute():
for i in range(1, 10):
await asyncio.sleep(60)
print(i, 'minute') loop = asyncio.get_event_loop()
loop.run_until_complete(
asyncio.gather(print_every_second(),
print_every_minute())
)
loop.close()

asyncio标准库2 Hello Clock的更多相关文章

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

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

  2. asyncio标准库7 Producer/consumer

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

  3. asyncio标准库6 Threads & Subprocess

    Threads import asyncio def compute_pi(digits): # implementation return 3.14 async def main(loop): di ...

  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标准库1 Hello World

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

  8. C语言-12-日期和时间处理标准库详细解析及示例

    概述 标准库 提供了用于日期和时间处理的结构和函数 是C++语言日期和时间处理的基础 与时间相关的类型 clock_t,本质是:unsigned long typedef unsigned long ...

  9. STL笔记(6)标准库:标准库中的排序算法

    STL笔记(6)标准库:标准库中的排序算法 标准库:标准库中的排序算法The Standard Librarian: Sorting in the Standard Library Matthew A ...

随机推荐

  1. LeetCode162.寻找峰值

    162.寻找峰值 描述 峰值元素是指其值大于左右相邻值的元素. 给定一个输入数组 nums,其中 nums[i] ≠ nums[i+1],找到峰值元素并返回其索引. 数组可能包含多个峰值,在这种情况下 ...

  2. 2018南京icpc现场赛心得

    第一次参加icpc的比赛,也是第一块奖牌,虽然只是铜,但其实打的已经很好了,稍微差一点就可以摸银了. 之前参加省赛,成为那次比赛我校唯一一个没拿奖的队伍,其实还是一直都有一些心结的,而这段时间和新的队 ...

  3. C# 关于utf-8的研究

    前提 如果一不小心把字符转成utf8的格式,但是却产生了乱码.这个时候要么就是寻找其他的转码方式,要么就不想要了,直接过滤吧. 这里说的是直接过滤的办法. 参考链接 https://netvignet ...

  4. 建立ionic3的环境

    看了好些例子,关于如何搭建ionic3的环境的,结果因为代理服务器的原因,弄好久才成功.前面的步骤网上随意可以找到的了,关键是ionic设置代理的地方,如果你的公司也需要代理才能到外网的话.... 1 ...

  5. Oracle9i之xmltype应用(1)

    oracle从9i开始支持一种新的数据类型-- xmltype,用于存储和管理xml数据,并提供了很多的functions,用来直接读取xml文档和管理节点.下面将介绍xmltype的一些基本使用. ...

  6. scrapy_redis之官网列子domz

    一.  domz.py from scrapy.linkextractors import LinkExtractor from scrapy.spiders import CrawlSpider, ...

  7. spark ALS 推荐算法参数说明

  8. vim源码编译启用python

    坑:只指定with-python-config-dir没有指定enable-pythoninterp是没有用的 ./configure --enable-pythoninterp --with-pyt ...

  9. yum安装git

    此方法对于RHEL.Fedora.CentOS有效: 1.yum install git 2.yum istall git-svn git-email git-gui gitk

  10. shell中if的可判断的类型

    -d :判断制定的是否为目录-z:判断制定的变量是否存在值-f:判断制定的是否为文件-L:判断制定的是否为符号链接-r:判断制定的是否可读-w:判断制定的是否可写-x:判断存在的对象是否可以执行!:测 ...