【问题标题】:How do I get the asincio current time? python我如何获得asincio当前时间? Python
【发布时间】:2023-02-18 18:24:17
【问题描述】:

我想异步获取当前时间

我有代码:

async def get_hour():
    return datetime.datetime.now().hour

async def timer():
    while True:
        print(await get_hour())
        await asyncio.sleep(60)

asyncio.run(timer())

他正在工作,但另一个代码不异步工作。

【问题讨论】:

    标签: python python-3.x datetime asynchronous python-asyncio


    【解决方案1】:

    只需使用常规的非异步操作即可。获取当前时间不是一些缓慢的 I/O 绑定操作,您需要让其他代码在它发生时运行。这就像我们没有加法或乘法的异步版本一样。

    【讨论】:

      猜你喜欢
      • 2010-09-22
      • 2014-09-10
      • 2017-08-02
      • 2017-10-01
      • 1970-01-01
      • 2020-08-04
      • 2011-09-24
      相关资源
      最近更新 更多