如何调度协程,并发运行

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. window环境下安装Python2和Python3

    一.  python 安装 1. 下载安装包 https://www.python.org/ftp/python/2.7.14/python-2.7.14.amd64.msi # 2.7安装包 htt ...

  2. linux安装PHP7以及扩展

    Linux下安装PHP7 事先升级gcc4.8,然后安装PHP7,安装步骤参考:CentOS安装PHP7 1.Linux下编译的php没有php.ini 解决办法:从源代码目录中复制php.ini-d ...

  3. Jquery 循环内间隔执行 异步执行

    <script type="text/javascript"> var arr = new Array(); var arrk = 0; jQuery(function ...

  4. Java线程池详解(一)

    一.线程池初探 所谓线程池,就是将多个线程放在一个池子里面(所谓池化技术),然后需要线程的时候不是创建一个线程,而是从线程池里面获取一个可用的线程,然后执行我们的任务.线程池的关键在于它为我们管理了多 ...

  5. 用poi替换ppt中的文字和图片

    try {            // 获取PPT文件             String pptModelPath =ConfigReadUtil.getInstance().getConfigI ...

  6. Laplace变换要点

    Laplace变换在求解微分方程.信号系统.自动控制领域都有重要作用.阅读<复变函数与积分变换>华中科大第三版,小结要点. 方便应用,最先给出变换表: 定义: 性质: 周期函数与卷积:

  7. linux拓展之 用linux命令 管理windows一秒完成不可思议的操作--本节实战find 移动!!

    花里胡哨的东西太多,有时候觉得简单也好! 你学习了Linux,是不是觉得Linux很强大!命令的多样性结合性有没有把你征服? 在那个烈日炎炎的夏日,我下载了辣末多老男孩的视屏----但是突然我只想看t ...

  8. (转)linux sed命令就是这么简单

    sed替换命令 原文:https://www.cnblogs.com/zd520pyx1314/p/6061337.html http://www.cnblogs.com/wangqiguo/p/67 ...

  9. TOJ 2641 Gene

    描述 How can millions of different and complex structures be built using only a few simple building bl ...

  10. js中的break,continue,return

    js中的break,continue, return (转) 面向对象编程语法中我们会碰到break ,continue, return这三个常用的关键字,那么关于这三个关键字的使用具体的操作是什么呢 ...