From bd064816fcc07ee1d94031973993f5a07b109f44 Mon Sep 17 00:00:00 2001 From: shenchucheng Date: Mon, 25 Sep 2023 00:48:47 +0800 Subject: [PATCH] Set WindowsProactorEventLoopPolicy after importing sk_function in python3.9+ --- metagpt/_compat.py | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/metagpt/_compat.py b/metagpt/_compat.py index 91bc1e5a1..c442bd7de 100644 --- a/metagpt/_compat.py +++ b/metagpt/_compat.py @@ -2,19 +2,22 @@ import platform import sys import warnings -if sys.implementation.name == "cpython" and platform.system() == "Windows" and sys.version_info[:2] == (3, 9): +if sys.implementation.name == "cpython" and platform.system() == "Windows": import asyncio - from asyncio.proactor_events import _ProactorBasePipeTransport - from semantic_kernel.orchestration import sk_function as _ # noqa: F401 + if sys.version_info[:2] == (3, 9): + from asyncio.proactor_events import _ProactorBasePipeTransport - # https://github.com/python/cpython/pull/92842 - def pacth_del(self, _warn=warnings.warn): - if self._sock is not None: - _warn(f"unclosed transport {self!r}", ResourceWarning, source=self) - self._sock.close() + # https://github.com/python/cpython/pull/92842 + def pacth_del(self, _warn=warnings.warn): + if self._sock is not None: + _warn(f"unclosed transport {self!r}", ResourceWarning, source=self) + self._sock.close() - _ProactorBasePipeTransport.__del__ = pacth_del + _ProactorBasePipeTransport.__del__ = pacth_del - # caused by https://github.com/microsoft/semantic-kernel/pull/1416 - asyncio.set_event_loop_policy(asyncio.WindowsProactorEventLoopPolicy()) + if sys.version_info >= (3, 9, 0): + from semantic_kernel.orchestration import sk_function as _ # noqa: F401 + + # caused by https://github.com/microsoft/semantic-kernel/pull/1416 + asyncio.set_event_loop_policy(asyncio.WindowsProactorEventLoopPolicy())