同步睡眠进入 asyncio 协程

2024-03-30

我有一个协程如下:

async def download():
    downloader = DataManager()
    downloader.download()

DataManager.download()方法如下:

def download(self):
    start_multiple_docker_containers()
    while True:
        check_containers_statuses()
        sleep(N)  # synchronous sleep from time module

这是一个好的做法吗?如果没有,我该如何使用asyncio.sleep in download()?

或者也许这样的代码结构在概念上是错误的?


这是我的解决方案:

import asyncio
import time


# Mocks of domain-specific functions
# ----------------------------------

def get_container_status(container_id, initial_time):
    """This mocks container status to change to 'exited' in 10 seconds"""
    if time.time() - initial_time < 10:
        print("%s: container %s still running" % (time.time(), container_id))
        return 'running'
    else:
        print("%s: container %s exited" % (time.time(), container_id))
        return 'exited'

def is_download_complete(container_id, initial_time):
    """This mocks download finished in 20 seconds after program's start"""
    if time.time() - initial_time < 20:
        print("%s: download from %s in progress" % (time.time(), container_id))
        return False
    else:
        print("%s: download from %s done" % (time.time(), container_id))
        return True

def get_downloaded_data(container_id):
    return "foo"


# Coroutines
# ----------

async def container_exited(container_id, initial_time):
    while True:
        await asyncio.sleep(1) # == setTimeout(1000), != sleep(1000)
        if get_container_status(container_id, initial_time) == 'exited':
            return container_id

async def download_data_by_container_id(container_id, initial_time):
    container_id = await container_exited(container_id, initial_time)
    while True:
        await asyncio.sleep(1)
        if is_download_complete(container_id, initial_time):
            return get_downloaded_data(container_id)


# Main loop
# ---------

if __name__ == "__main__":

    initial_time = time.time()

    loop = asyncio.get_event_loop()

    tasks = [
        asyncio.ensure_future(download_data_by_container_id("A", initial_time)),
        asyncio.ensure_future(download_data_by_container_id("B", initial_time))
    ]

    loop.run_until_complete(asyncio.wait(tasks))

    loop.close()

结果是:

1487334722.321165: container A still running
1487334722.321412: container B still running
1487334723.325897: container A still running
1487334723.3259578: container B still running
1487334724.3285959: container A still running
1487334724.328662: container B still running
1487334725.3312798: container A still running
1487334725.331337: container B still running
1487334726.3340318: container A still running
1487334726.33409: container B still running
1487334727.336779: container A still running
1487334727.336842: container B still running
1487334728.339425: container A still running
1487334728.339506: container B still running
1487334729.34211: container A still running
1487334729.342168: container B still running
1487334730.3448708: container A still running
1487334730.34493: container B still running
1487334731.34754: container A exited
1487334731.347598: container B exited
1487334732.350253: download from A in progress
1487334732.3503108: download from B in progress
1487334733.354369: download from A in progress
1487334733.354424: download from B in progress
1487334734.354686: download from A in progress
1487334734.3548028: download from B in progress
1487334735.358371: download from A in progress
1487334735.358461: download from B in progress
1487334736.3610592: download from A in progress
1487334736.361115: download from B in progress
1487334737.363115: download from A in progress
1487334737.363211: download from B in progress
1487334738.3664992: download from A in progress
1487334738.36656: download from B in progress
1487334739.369131: download from A in progress
1487334739.36919: download from B in progress
1487334740.371079: download from A in progress
1487334740.37119: download from B in progress
1487334741.374521: download from A done
1487334741.3745651: download from B done

至于sleep()函数 - 不,你不应该使用它。它会阻塞整个 python 解释器 1 秒,这不是你想要的。

请记住,你没有并行性(线程等),你有并发性.

IE。你有一个只有 1 个执行线程的 python 解释器,主循环和所有协程都在其中运行,相互抢占。您希望解释器将 99.999% 的工作时间花在由 asyncio 创建的主循环中,轮询套接字并等待超时。

所有协程都应该尽快返回并且绝对不应该包含阻塞sleep- 如果您调用它,它会阻止整个解释器并阻止主循环从套接字获取信息或运行协程以响应数据到达这些套接字。

所以,你应该等待asyncio.sleep()这本质上相当于 Javascript 的setTimeout()- 它只是告诉主循环,在特定的时间它应该唤醒这个协程并继续运行它。


建议阅读:

  • https://snarky.ca/how-the-heck-does-async-await-work-in-python-3-5/ https://snarky.ca/how-the-heck-does-async-await-work-in-python-3-5/
  • https://docs.python.org/3/library/asyncio.html https://docs.python.org/3/library/asyncio.html
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

同步睡眠进入 asyncio 协程 的相关文章

随机推荐

  • C++ 模板参数更改对指针的引用

    也许我不知道如何搜索 但事实上我找不到任何人谈论这个 我的结构有一个non type论证取决于type争论 template lt typename SpecType SpecType NonType gt struct Struct Wh
  • yii 自定义错误页面,例如 404、403、500

    我正在尝试为所有错误消息创建单独的文件 404 403 500 等 这样我就可以为它们进行定制设计 如果可能的话 我不希望页眉和页脚也包含在我的错误页面中 现在我有这个 在我的站点控制器 php并把错误404 php进入我的浏览量 站点 f
  • 如何分析 Java 中的线程?

    我的应用程序中有生产者和消费者线程 我需要对它们进行分析以查看线程的性能 每个线程进入睡眠和等待之前所花费的时间等 并采取纠正措施以提高应用程序的整体效率 关于如何解决这个问题有什么建议吗 我个人使用 YourKit java profil
  • 在 Django 中接受电子邮件地址作为用户名

    有没有一种好方法可以在 Django 中执行此操作 而无需滚动我自己的身份验证系统 我希望用户名是用户的电子邮件地址 而不是他们创建用户名 请指教 对于其他想要这样做的人 我建议看一下django 电子邮件作为用户名 https githu
  • 使用 docker 和 --privileged 构建

    我在用着本指南 https aws amazon com blogs ai build a voice kit with amazon lex and a raspberry pi 使用 Amazon Lex 和 Raspberry Pi
  • 如何在VB6中模拟.net Int64?

    如何在 VB6 中存储 Int64 数字以与 Win32 函数一起使用 有没有办法在 net中定义像Int64这样的类型 并简单评估一下数字 我认为许多 VB6 程序员都需要这样的东西 因为一些 Win32 API 使用 int64 作为参
  • 如何淡出图像的侧面?

    当您扩大浏览器时 您会注意到图像的右侧和左侧以黑色淡出 我需要将相同的功能应用到我的画廊 但不知道 我找到了这个 gt gt link https stackoverflow com questions 9437400 css fade o
  • 不带 href="#" 的脚本链接

    我的网络应用程序使用了很多 a href Perform client script action a 用于脚本编写的锚链接 这是我十多年前作为 Dreamweaver 用户时养成的习惯 现在我开始质疑这是否有必要 我其实不知道为什么 a
  • 我应该在 SSL 证书请求的通用名称中包含“www”吗? [关闭]

    Closed 这个问题是无关 help closed questions 目前不接受答案 我即将为一个小型网站购买 SSL 证书 输入 www domain Name com 或 domainName com 作为常用名是最佳做法吗 确保您
  • `ggplot2` axis.text margin 修改了刻度位置

    我不确定我是否遇到了错误或者操作不正确 指定时axis textggplot 中的边距 并移动轴的位置 设置不会保留 在不移动轴文本的情况下 轴周围有足够的空间 library ggplot ggplot mtcars aes mpg wt
  • Google 主页网址 https://www.google.co.in/?gws_rd=cr&ei=xgH... 中的“ei”是什么意思?

    我知道谷歌网址中的一些参数 例如 gws 谷歌网络服务器 rd 重定向 cr 所指国家 但我注意到一个我无法识别的 ei 什么是ei这个网址是什么意思 http www google co in url ei qWwlUqD4MIrRrQf
  • 异步方法的并发执行

    使用 async await 模型 我有一个方法可以对 Web 服务进行 3 次不同的调用 然后返回结果的并集 var result1 await myService GetData source1 var result2 await my
  • 通过 RFC 5987 处理带空格的 filename* 参数会导致文件名中出现“+”

    我正在处理一些遗留代码 所以不 我不能只使用带有编码文件名组件的 URL 允许用户从我们的网站下载文件 由于我们的文件名通常采用多种不同的语言 因此它们都存储为 UTF 8 我编写了一些代码来处理 RFC5987 转换为正确的 filena
  • 让 JSF 从 EL 访问 Map 值而不是 bean 字段?

    是否有任何正确的方法来覆盖 JSF 从表达式语言访问 beans 字段的方式 这个想法是模仿这种行为以便访问Map
  • 在 Windows 7 中删除超过 10 天的文件夹的批处理文件

    我想创建一个批处理文件 该文件应使用 Windows 7 删除文件夹中超过 10 天的所有子文件夹 任何帮助 将不胜感激 改编自这是对一个非常相似问题的回答 https stackoverflow com questions 3419091
  • 从整数列表中获取随机数

    如果我有一个整数列表 List
  • 计算出的字体大小大于 Asus Nexus 7 上 CSS 中定义的字体大小

    我在 Asus Nexus 7 用 jQuery Mobile 编写 上运行该网页 我在 CSS 中将字体大小设置为 14px 但在调试过程中 在 PC 上使用 Chrome 我可以看到它的计算大小是 22px HTML 代码如下所示 di
  • Kubernetes 和 AWS:设置 LoadBalancer 以使用预定义的安全组

    正如标题所示 我正在寻找一种方法来强制 LoadBalancer 服务使用 AWS 中预定义的安全组 我不想手动编辑 Kubernetes 为 ELB 创建的安全组的入站 出站规则 我无法在文档中找到任何内容 也没有找到任何可以在网上其他地
  • 使用 jinja 的 Ansible set_fact 列表

    我正在尝试学习 jinja 和 Ansible 这是在 RHEL 7 9 上 ansible 2 9 27 ansible python module location usr lib python2 7 site packages ans
  • 同步睡眠进入 asyncio 协程

    我有一个协程如下 async def download downloader DataManager downloader download DataManager download 方法如下 def download self start