fix: update asyncio event loop policy on Windows (#251)
* fix: update asyncio event loop policy on Windows * Use `platform.system` instead of `sys.platform` --------- Co-authored-by: Waket Zheng <waketzheng@gmail.com>
This commit is contained in:
parent
1793dab43d
commit
7f8c5dcddc
@ -1,6 +1,7 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
import platform
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import TYPE_CHECKING
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
@ -26,6 +27,29 @@ if TYPE_CHECKING:
|
|||||||
from aerich.inspectdb import Inspect
|
from aerich.inspectdb import Inspect
|
||||||
|
|
||||||
|
|
||||||
|
def _init_asyncio_patch():
|
||||||
|
"""
|
||||||
|
Select compatible event loop for psycopg3.
|
||||||
|
|
||||||
|
As of Python 3.8+, the default event loop on Windows is `proactor`,
|
||||||
|
however psycopg3 requires the old default "selector" event loop.
|
||||||
|
See https://www.psycopg.org/psycopg3/docs/advanced/async.html
|
||||||
|
"""
|
||||||
|
if platform.system() == "Windows":
|
||||||
|
try:
|
||||||
|
from asyncio import WindowsSelectorEventLoopPolicy
|
||||||
|
except ImportError:
|
||||||
|
pass # Can't assign a policy which doesn't exist.
|
||||||
|
else:
|
||||||
|
from asyncio import get_event_loop_policy, set_event_loop_policy
|
||||||
|
|
||||||
|
if not isinstance(get_event_loop_policy(), WindowsSelectorEventLoopPolicy):
|
||||||
|
set_event_loop_policy(WindowsSelectorEventLoopPolicy())
|
||||||
|
|
||||||
|
|
||||||
|
_init_asyncio_patch()
|
||||||
|
|
||||||
|
|
||||||
class Command:
|
class Command:
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user