Skip to content

Commit

Permalink
Use uvloop instead of asyncio default event loop (#818)
Browse files Browse the repository at this point in the history
* Add uvloop to dependencies

* Install uvloop in main based on config

* Use loop.create_future instead of constructing asyncio.Future manually

Technically this doesn't matter for uvloop because it returns asyncio.Future,
however, its still good practice and might matter in the future.
  • Loading branch information
Askaholic authored Aug 8, 2021
1 parent 44d80da commit b977205
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 134 deletions.
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ aiomysql = {editable = true, git = "https://github.com/aio-libs/aiomysql"}
pyyaml = "*"
aio_pika = "*"
pyjwt = {version = ">=2", extras = ["crypto"]}
uvloop = "*"

[dev-packages]
pytest = "*"
Expand Down
280 changes: 150 additions & 130 deletions Pipfile.lock

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion server.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@

async def main():
loop = asyncio.get_running_loop()
done = asyncio.Future()
done = loop.create_future()

logger.info("Event loop: %s", loop)

def signal_handler(sig: int, _frame):
logger.info(
Expand Down Expand Up @@ -129,4 +131,8 @@ async def restart_control_server():
logger.addHandler(stderr_handler)
logger.setLevel(config.LOG_LEVEL)

if config.USE_UVLOOP:
import uvloop
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())

asyncio.run(main())
3 changes: 3 additions & 0 deletions server/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ def __init__(self):
"""
self.CONFIGURATION_REFRESH_TIME = 300
self.LOG_LEVEL = "DEBUG"
# Whether or not to use uvloop as a drop-in replacement for asyncio's
# default event loop
self.USE_UVLOOP = True
self.PROFILING_COUNT = 300
self.PROFILING_DURATION = 2
self.PROFILING_INTERVAL = -1
Expand Down
5 changes: 3 additions & 2 deletions server/games/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,9 @@ def __init__(
"RestrictedCategories": 0
}
self.mods = {}
self._is_hosted = asyncio.Future()
self._launch_fut = asyncio.Future()
loop = asyncio.get_event_loop()
self._is_hosted = loop.create_future()
self._launch_fut = loop.create_future()

self._logger.debug("%s created", self)
asyncio.get_event_loop().create_task(self.timeout_game(setup_timeout))
Expand Down
2 changes: 1 addition & 1 deletion server/matchmaker/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def __init__(
self.players = players
self.rating_type = rating_type
self.start_time = start_time or time.time()
self._match = asyncio.Future()
self._match = asyncio.get_event_loop().create_future()
self._failed_matching_attempts = 0
self.on_matched = on_matched

Expand Down

0 comments on commit b977205

Please sign in to comment.