diff --git a/README.md b/README.md index 17ab5d6..007e100 100644 --- a/README.md +++ b/README.md @@ -363,25 +363,41 @@ In order to use the async version, you need to install the package using: ``` pip install discord-webhook[async] ``` +python-discord-webhook supports both asyncio and [trio](https://trio.readthedocs.io/en/stable/) through the use of [anyio](https://anyio.readthedocs.io/en/3.x/) + Example usage: ```python import asyncio -from discord_webhook import AsyncDiscordWebhook +import os + +import trio +import discord_webhook -async def send_webhook(message): - webhook = AsyncDiscordWebhook(url="your webhook url", content=message) + +async def send_webhook(message: str): + webhook = discord_webhook.AsyncDiscordWebhook(url="your webhook url", content=message) await webhook.execute() -async def main(): +async def trio_main(): + async with trio.open_nursery() as nursery: + nursery.start_soon(send_webhook, "Async webhook message 1") + nursery.start_soon(send_webhook, "Async webhook message 2") + + +async def asyncio_main(): await asyncio.gather( send_webhook("Async webhook message 1"), send_webhook("Async webhook message 2"), ) # sends both messages asynchronously -asyncio.run(main()) +if __name__ == "__main__": + trio.run(trio_main) + asyncio.run(asyncio_main()) + + ``` ### Use CLI diff --git a/discord_webhook/async_webhook.py b/discord_webhook/async_webhook.py index d66bc73..0b2b41a 100644 --- a/discord_webhook/async_webhook.py +++ b/discord_webhook/async_webhook.py @@ -1,4 +1,4 @@ -import asyncio +import anyio import json import logging from contextlib import asynccontextmanager @@ -90,7 +90,7 @@ async def handle_rate_limit(self, response, request) -> "httpx.Response": wh_sleep=round(wh_sleep, 2) ) ) - await asyncio.sleep(wh_sleep) + await anyio.sleep(wh_sleep) response = await request() if response.status_code in [200, 204]: return response diff --git a/pyproject.toml b/pyproject.toml index 1454cfd..d2edb1b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -14,6 +14,7 @@ keywords = ["discord", "webhook"] python = "^3.8" requests = "^2.28.1" httpx = { version = "^0.23.0", optional = true } +anyio = "^3.7.1" [tool.poetry.extras] async = ["httpx"]