如何调度协程,并发运行

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. docker 镜像的配置文件修改

    #抛砖引玉# docker exec -ti 容器ID /bin/bash

  2. HDU - 1427 / UESTC - 1252 经典dfs

    很好奇为什么hzwer那种稍改一下还是无法过样例,代码我没看出问题 换了一种用桶组合挑取两个数不断回溯的做法 这是HDU1427的代码,后者改一改就行了 #include<bits/stdc++ ...

  3. Q942 增减字符串匹配

    给定只含 "I"(增大)或 "D"(减小)的字符串 S ,令 N = S.length. 返回 [0, 1, ..., N] 的任意排列 A 使得对于所有 i ...

  4. sqlmap命令小结

    --technique 这个参数可以指定sqlmap使用的探测技术,默认情况下会测试所有的方式. 支持的探测方式如下: B: Boolean-based blind SQL injection(布尔型 ...

  5. mybatis-dao开发

    学而时习之,不亦说乎!                              --<论语> 本文以前文“mybatis-入门”为基础,同时再次提醒最佳参考资料: http://www. ...

  6. 在Linux系统中,使用useradd命令新建用户后,登录该用户时shell开头为$,不显示用户名和路径,如下:

    在~/.bash_profile中加入以下代码,若无该文件可自行创建: vi ~/.bash_profile #加入 #export PS1='[u@h W]$' 大写W代表最后路径,小写w代表详细路 ...

  7. Oracle RAC集群搭建(二)-基础环境配置

    01,创建用户,用户组 [root@rac1 ~]# groupadd -g 501 oinstall [root@rac1 ~]# groupadd -g 502 dba [root@rac1 ~] ...

  8. Git学习手记

    直接使用github的客户端即可 1.简介 集中化的版本控制系统( Centralized Version Control Systems,简称 CVCS )应运而生.这类系统,诸如 CVS,Subv ...

  9. hibernate框架的搭建

    1 导入所需的jar包 1 导入hibernate必须的jar包 2 导入驱动包 2 创建数据库,准备表,实体 1 创建hibernate数据库 CREATE DATABASE hibernate; ...

  10. IE678不兼容CSS3 user-select:none(不可复制功能),需要JS解决

    [方法一:CSS3实现文本不可复制] .content {-moz-user-select:none;-webkit-user-select:none;-ms-user-select:none;-o- ...