Skip to content

Commit

Permalink
Clean up asyncio loop usage
Browse files Browse the repository at this point in the history
  • Loading branch information
linuxdaemon committed Apr 9, 2024
1 parent c20952f commit 14985bc
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 9 deletions.
2 changes: 1 addition & 1 deletion cloudbot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def __init__(
self.db_engine = create_engine(db_path)
database.configure(self.db_engine)
self.db_executor_pool = ExecutorPool(
50, max_workers=1, thread_name_prefix="cloudbot-db"
50, max_workers=1, thread_name_prefix="cloudbot-db", loop=self.loop
)

logger.debug("Database system initialised.")
Expand Down
3 changes: 1 addition & 2 deletions cloudbot/util/async_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ def run_coroutine_threadsafe(coro, loop):
asyncio.run_coroutine_threadsafe(coro, loop)


def create_future(loop=None):
loop = loop if loop is not None else asyncio.get_event_loop()
def create_future(loop):
return loop.create_future()


Expand Down
7 changes: 4 additions & 3 deletions cloudbot/util/executor_pool.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from asyncio import AbstractEventLoop
import logging
import os
import random
Expand Down Expand Up @@ -27,8 +28,8 @@ def __del__(self):

class ExecutorPool:
def __init__(
self, max_executors=None, executor_type=ThreadPoolExecutor, **kwargs
):
self, max_executors=None, executor_type=ThreadPoolExecutor, *, loop:AbstractEventLoop, **kwargs
) -> None:
if max_executors is None:
max_executors = (os.cpu_count() or 1) * 5

Expand All @@ -41,7 +42,7 @@ def __init__(

self._executors = []
self._free_executors = []
self._executor_waiter = create_future()
self._executor_waiter = create_future(loop)

def get(self):
return ExecutorWrapper(self, self._get())
Expand Down
10 changes: 7 additions & 3 deletions tests/util/mock_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,13 @@ def __init__(
db: Optional[MockDB] = None,
base_dir=None,
):
self.db_executor_pool = ExecutorPool(
50, max_workers=1, thread_name_prefix="cloudbot-db"
)
if loop:
self.db_executor_pool = ExecutorPool(
50, max_workers=1, thread_name_prefix="cloudbot-db", loop=loop
)
else:
self.db_executor_pool = None

self.old_db = None
self.do_db_migrate = False
self.loop = loop
Expand Down

0 comments on commit 14985bc

Please sign in to comment.