【问题标题】:How to call sync methods from other module In the form of async method in Discord.py?如何在 Discord.py 中以异步方法的形式从其他模块调用同步方法?
【发布时间】:2021-10-20 04:52:22
【问题描述】:

我想在discord.py中使用Python Twitch API模块完全异步,如果不可行或者需要很长时间,请介绍完整的替代异步模块。

【问题讨论】:

标签: python asynchronous async-await discord.py python-asyncio


【解决方案1】:

asyncio 不禁止在async 函数中使用sync 函数,因此您可以在 discord.py 事件中使用您的库

import asyncio

# Synchronous function
def say_sync(string):
  print(string)

# Asynchronous function
async def async_function():
  say_sync("Hello")
  await asyncio.sleep(1) # Sleep for 1 second
  say_sync("world!")

asyncio.run(async_function()) # Run async function

Live Example

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-23
    • 2019-05-26
    • 1970-01-01
    • 2017-03-06
    • 2020-12-19
    • 2021-05-29
    • 1970-01-01
    相关资源
    最近更新 更多