diff options
-rw-r--r-- | .pre-commit-config.yaml | 2 | ||||
-rw-r--r-- | bot/resources/tags/blocking.md | 28 | ||||
-rw-r--r-- | config-default.yml | 6 |
3 files changed, 32 insertions, 4 deletions
diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 131ba9453..a9412f07d 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -18,7 +18,7 @@ repos: - id: flake8 name: Flake8 description: This hook runs flake8 within our project's environment. - entry: poetry run task flake8 + entry: poetry run flake8 language: system types: [python] require_serial: true diff --git a/bot/resources/tags/blocking.md b/bot/resources/tags/blocking.md new file mode 100644 index 000000000..31d91294c --- /dev/null +++ b/bot/resources/tags/blocking.md @@ -0,0 +1,28 @@ +**Why do we need asynchronous programming?** + +Imagine that you're coding a Discord bot and every time somebody uses a command, you need to get some information from a database. But there's a catch: the database servers are acting up today and take a whole 10 seconds to respond. If you do **not** use asynchronous methods, your whole bot will stop running until it gets a response from the database. How do you fix this? Asynchronous programming. + +**What is asynchronous programming?** + +An asynchronous program utilises the `async` and `await` keywords. An asynchronous program pauses what it's doing and does something else whilst it waits for some third-party service to complete whatever it's supposed to do. Any code within an `async` context manager or function marked with the `await` keyword indicates to Python, that whilst this operation is being completed, it can do something else. For example: + +```py +import discord + +# Bunch of bot code + +async def ping(ctx): + await ctx.send("Pong!") +``` + +**What does the term "blocking" mean?** + +A blocking operation is wherever you do something without `await`ing it. This tells Python that this step must be completed before it can do anything else. Common examples of blocking operations, as simple as they may seem, include: outputting text, adding two numbers and appending an item onto a list. Most common Python libraries have an asynchronous version available to use in asynchronous contexts. + +**`async` libraries** + +The standard async library - `asyncio` +Asynchronous web requests - `aiohttp` +Talking to PostgreSQL asynchronously - `asyncpg` +MongoDB interactions asynchronously - `motor` +Check out [this](https://github.com/timofurrer/awesome-asyncio) list for even more! diff --git a/config-default.yml b/config-default.yml index 30626c811..394c51c26 100644 --- a/config-default.yml +++ b/config-default.yml @@ -56,9 +56,9 @@ style: failmail: "<:failmail:633660039931887616>" - incident_actioned: "<:incident_actioned:719645530128646266>" - incident_investigating: "<:incident_investigating:719645658671480924>" - incident_unactioned: "<:incident_unactioned:719645583245180960>" + incident_actioned: "<:incident_actioned:714221559279255583>" + incident_investigating: "<:incident_investigating:714224190928191551>" + incident_unactioned: "<:incident_unactioned:714223099645526026>" status_dnd: "<:status_dnd:470326272082313216>" status_idle: "<:status_idle:470326266625785866>" |