From 2b85ccd470b902febf2bec2a66c47761ed279b92 Mon Sep 17 00:00:00 2001 From: Li Date: Fri, 16 Jun 2023 08:31:37 +0800 Subject: [PATCH] Python: Fix windows runtime error: adapt asyncio event loop policy for windows python 3.10 and above (#1416) ### Motivation and Context To fix a runtime error on windows for python version 3.10, 3.11 and above On windows, python version above 3.10.1, there will be a runtime error caused by asyncio event loop. add adapting code for windows when python version is above 3.10.1 to avoid the error. ### Description Refer link: https://docs.python.org/3/library/asyncio-policy.html "Note In Python versions 3.10.9, 3.11.1 and 3.12 the [get_event_loop()](https://docs.python.org/3/library/asyncio-eventloop.html#asyncio.get_event_loop) method of the default asyncio policy emits a [DeprecationWarning](https://docs.python.org/3/library/exceptions.html#DeprecationWarning) if there is no running event loop and no current loop is set. In some future Python release this will become an error." Note: per testing, this error also occurred on python 3.10 below 3.10.9 Co-authored-by: Mark Karle Co-authored-by: Lee Miller --- python/semantic_kernel/orchestration/sk_function.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/python/semantic_kernel/orchestration/sk_function.py b/python/semantic_kernel/orchestration/sk_function.py index af0cbbbc3bb7..17af6d375cfa 100644 --- a/python/semantic_kernel/orchestration/sk_function.py +++ b/python/semantic_kernel/orchestration/sk_function.py @@ -1,6 +1,8 @@ # Copyright (c) Microsoft. All rights reserved. import asyncio +import platform +import sys import threading from enum import Enum from logging import Logger @@ -36,6 +38,9 @@ ) from semantic_kernel.utils.null_logger import NullLogger +if platform.system() == "Windows" and sys.version_info >= (3, 8, 0): + asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()) + class SKFunction(SKFunctionBase): """