From e5858fecb3eb0713e9c6ab66b52c1a98cb800ffa Mon Sep 17 00:00:00 2001 From: 404 <34942042+neko404notfound@users.noreply.github.com> Date: Mon, 21 May 2018 13:54:54 +0100 Subject: [PATCH] Added optional uvloop support. uvloop uses libs used by node.js and has been shown to be a much more efficient event loop policy than the default Python asyncio implementation. If uvloop is installed, this is quietly changed before the bot is initialised. If uvloop cannot be set, either from not being installed, or from being used on an unsupported system such as Windoze, then nothing is changed. The only change should be a slight increase in throughput if it is detected. Please test first. --- run.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/run.py b/run.py index 3342e7d..5b81fb4 100644 --- a/run.py +++ b/run.py @@ -2,6 +2,7 @@ # -*- coding: utf8 -*- # Import packages +import asyncio import discord from discord.ext import commands import json @@ -11,6 +12,18 @@ import random # Import custom files from src.config.config import LoadConfig +# If uvloop is installed, change to that eventloop policy as it +# is more efficient +try: + import uvloop + asyncio.set_event_loop_policy(uvloop.EventLoopPolicy()) + del uvloop +except BaseException as ex: + print(f'Could not load uvloop. {type(ex).__name__}: {ex};', + 'reverting to default impl.') +else: + print(f'Using uvloop for asyncio event loop policy.') + # Bot Class class SebiMachine(commands.Bot, LoadConfig): @@ -67,4 +80,4 @@ if __name__ == '__main__': with open('src/config/PrivateConfig.json') as fp: PrivateConfig = json.load(fp) fp.close() - client.run(PrivateConfig["bot-key"]) \ No newline at end of file + client.run(PrivateConfig["bot-key"])