Skip to content

Commit

Permalink
avoid deprecated asyncio.get_event_loop
Browse files Browse the repository at this point in the history
  • Loading branch information
graingert committed Jun 4, 2021
1 parent 87da6cf commit 189f9ef
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 16 deletions.
16 changes: 2 additions & 14 deletions uvicorn/loops/asyncio.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,7 @@
import asyncio
import platform
import selectors
import sys


def asyncio_setup() -> None: # pragma: no cover
loop: asyncio.AbstractEventLoop
if (
sys.version_info.major >= 3
and sys.version_info.minor >= 8
and platform.system() == "Windows"
):
selector = selectors.SelectSelector()
loop = asyncio.SelectorEventLoop(selector)
asyncio.set_event_loop(loop)
else:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
if sys.version_info >= (3, 8) and sys.platform == "win32":
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
6 changes: 4 additions & 2 deletions uvicorn/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ def __init__(self, config):

def run(self, sockets=None):
self.config.setup_event_loop()
loop = asyncio.get_event_loop()
loop.run_until_complete(self.serve(sockets=sockets))
if sys.version_info > (3, 6):
return asyncio.run(self.serve(sockets=sockets))
else:
return asyncio.get_event_loop().run_until_complete(self.serve(sockets=sockets))

async def serve(self, sockets=None):
process_id = os.getpid()
Expand Down

0 comments on commit 189f9ef

Please sign in to comment.