import discord from contextvars_executor import ContextVarExecutor from classes.bot import Bot if sys.platform == "linux": # uvloop requires linux import uvloop asyncio.set_event_loop_policy(uvloop.EventLoopPolicy()) # Set the timezone to UTC os.environ["TZ"] = "UTC" bot = Bot( case_insensitive=True, status=discord.Status.idle, description="The one and only IdleRPG bot for discord", shard_ids=loads(sys.argv[1]), shard_count=int(sys.argv[2]), cluster_name=sys.argv[4], max_messages= 10000, # We have a ton of incoming messages, higher cache means commands like activeadventure # or guild adventure joining will stay in cache so reactions are counted ) if __name__ == "__main__": loop = asyncio.get_event_loop() loop.set_default_executor(ContextVarExecutor()) loop.run_until_complete(bot.connect_all())
def executor(request): e = ContextVarExecutor() yield e e.shutdown(wait=True)
def run_in_threadpool(func): @functools.wraps(func) def wrap(*args, **kwargs): def inner(): return func(*args, **kwargs) return asyncio.get_running_loop().run_in_executor(None, inner) return wrap current_session = scoped_session(Session, scopefunc=scopefunc) context_executor = ContextVarExecutor() async def example(): loop = asyncio.get_event_loop() loop.set_default_executor(context_executor) _scope.set(1) d = Dialog('some_user_id') data = d, Message(d, 'hello'), Message(d, 'again') @run_in_threadpool def query(): current_session.add_all(data) current_session.flush()
def _init_context(app: FastAPI) -> None: app.middleware('http')(context_middleware) loop = asyncio.get_event_loop() loop.set_default_executor(ContextVarExecutor(config.THREAD_POOL_SIZE))