Skip to content

Commit 7f8c5dc

Browse files
radluzwaketzheng
andauthored
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 <[email protected]>
1 parent 1793dab commit 7f8c5dc

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

aerich/__init__.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import os
4+
import platform
45
from pathlib import Path
56
from typing import TYPE_CHECKING
67

@@ -26,6 +27,29 @@
2627
from aerich.inspectdb import Inspect
2728

2829

30+
def _init_asyncio_patch():
31+
"""
32+
Select compatible event loop for psycopg3.
33+
34+
As of Python 3.8+, the default event loop on Windows is `proactor`,
35+
however psycopg3 requires the old default "selector" event loop.
36+
See https://www.psycopg.org/psycopg3/docs/advanced/async.html
37+
"""
38+
if platform.system() == "Windows":
39+
try:
40+
from asyncio import WindowsSelectorEventLoopPolicy
41+
except ImportError:
42+
pass # Can't assign a policy which doesn't exist.
43+
else:
44+
from asyncio import get_event_loop_policy, set_event_loop_policy
45+
46+
if not isinstance(get_event_loop_policy(), WindowsSelectorEventLoopPolicy):
47+
set_event_loop_policy(WindowsSelectorEventLoopPolicy())
48+
49+
50+
_init_asyncio_patch()
51+
52+
2953
class Command:
3054
def __init__(
3155
self,

0 commit comments

Comments
 (0)