From b2dbc55c7954d6dca064b68e03f807c397bc02de Mon Sep 17 00:00:00 2001 From: shadeofblue Date: Thu, 22 Oct 2020 11:23:29 +0200 Subject: [PATCH 1/6] add a work-around to make the examples work on win+py3.6/3.7 --- examples/blender/blender.py | 2 ++ examples/utils.py | 10 ++++++++++ examples/yacat/yacat.py | 2 ++ 3 files changed, 14 insertions(+) diff --git a/examples/blender/blender.py b/examples/blender/blender.py index e9b328ec6..22f49c4db 100755 --- a/examples/blender/blender.py +++ b/examples/blender/blender.py @@ -86,6 +86,8 @@ async def worker(ctx: WorkContext, tasks): parser.set_defaults(log_file="blender-yapapi.log") args = parser.parse_args() + utils.asyncio_fix() + enable_default_logger(log_file=args.log_file) loop = asyncio.get_event_loop() subnet = args.subnet_tag diff --git a/examples/utils.py b/examples/utils.py index f121fdccf..5e241c029 100644 --- a/examples/utils.py +++ b/examples/utils.py @@ -1,5 +1,7 @@ """Utilities for yapapi example scripts.""" import argparse +import asyncio +import sys TEXT_COLOR_RED = "\033[31;1m" TEXT_COLOR_GREEN = "\033[32;1m" @@ -21,3 +23,11 @@ def build_parser(description: str): "--log-file", default=None, help="Log file for YAPAPI; default: %(default)s" ) return parser + +def asyncio_fix(): + + class _WindowsEventPolicy(asyncio.events.BaseDefaultEventLoopPolicy): + _loop_factory = asyncio.windows_events.ProactorEventLoop + + if sys.platform == 'win32': + asyncio.set_event_loop_policy(_WindowsEventPolicy()) diff --git a/examples/yacat/yacat.py b/examples/yacat/yacat.py index ae0399b5f..1abeeb459 100644 --- a/examples/yacat/yacat.py +++ b/examples/yacat/yacat.py @@ -140,6 +140,8 @@ async def worker_find_password(ctx: WorkContext, tasks): args = parser.parse_args() + utils.asyncio_fix() + enable_default_logger(log_file=args.log_file) sys.stderr.write( From 3488f598112cb244536263afa5c091822507139b Mon Sep 17 00:00:00 2001 From: shadeofblue Date: Thu, 22 Oct 2020 11:34:13 +0200 Subject: [PATCH 2/6] fix --- examples/utils.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/examples/utils.py b/examples/utils.py index 5e241c029..d1a91fa1a 100644 --- a/examples/utils.py +++ b/examples/utils.py @@ -25,9 +25,8 @@ def build_parser(description: str): return parser def asyncio_fix(): - - class _WindowsEventPolicy(asyncio.events.BaseDefaultEventLoopPolicy): - _loop_factory = asyncio.windows_events.ProactorEventLoop - if sys.platform == 'win32': + class _WindowsEventPolicy(asyncio.events.BaseDefaultEventLoopPolicy): + _loop_factory = asyncio.windows_events.ProactorEventLoop + asyncio.set_event_loop_policy(_WindowsEventPolicy()) From 8ca5d7e922fc11e4233dba8df0ac6c5721d57e93 Mon Sep 17 00:00:00 2001 From: shadeofblue Date: Thu, 22 Oct 2020 11:44:37 +0200 Subject: [PATCH 3/6] black --- examples/utils.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/examples/utils.py b/examples/utils.py index d1a91fa1a..130b5402b 100644 --- a/examples/utils.py +++ b/examples/utils.py @@ -24,8 +24,10 @@ def build_parser(description: str): ) return parser + def asyncio_fix(): - if sys.platform == 'win32': + if sys.platform == "win32": + class _WindowsEventPolicy(asyncio.events.BaseDefaultEventLoopPolicy): _loop_factory = asyncio.windows_events.ProactorEventLoop From 2dbdc03b12653824220469e839950527b663fb28 Mon Sep 17 00:00:00 2001 From: shadeofblue Date: Thu, 22 Oct 2020 11:57:00 +0200 Subject: [PATCH 4/6] bump the version --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 27bbb15b8..71ec7a9e3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "poetry.masonry.api" [tool.poetry] name = "yapapi" -version = "0.3.0" +version = "0.3.1-alpha.0" description = "High-level Python API for the New Golem" authors = ["Przemysław K. Rekucki "] license = "LGPL-3.0-or-later" From e762288461f4fcb85bf40d8e411bb4fa105a336c Mon Sep 17 00:00:00 2001 From: shadeofblue Date: Thu, 22 Oct 2020 13:58:19 +0200 Subject: [PATCH 5/6] limit the fix to python 3.6/3.7 --- examples/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/utils.py b/examples/utils.py index 130b5402b..f2d75e42e 100644 --- a/examples/utils.py +++ b/examples/utils.py @@ -26,7 +26,7 @@ def build_parser(description: str): def asyncio_fix(): - if sys.platform == "win32": + if sys.platform == "win32" and sys.version_info[0] == 3 and sys.version_info[1] <= 7: class _WindowsEventPolicy(asyncio.events.BaseDefaultEventLoopPolicy): _loop_factory = asyncio.windows_events.ProactorEventLoop From 8e8809d88370dc7ddeda6525bec67731bc903b4c Mon Sep 17 00:00:00 2001 From: azawlocki Date: Fri, 23 Oct 2020 12:06:47 +0200 Subject: [PATCH 6/6] Rename `utils.asyncio_fix` to `utils.windows_event_loop_fix` --- examples/blender/blender.py | 3 ++- examples/utils.py | 7 +++++-- examples/yacat/yacat.py | 3 ++- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/examples/blender/blender.py b/examples/blender/blender.py index 22f49c4db..88d035709 100755 --- a/examples/blender/blender.py +++ b/examples/blender/blender.py @@ -86,7 +86,8 @@ async def worker(ctx: WorkContext, tasks): parser.set_defaults(log_file="blender-yapapi.log") args = parser.parse_args() - utils.asyncio_fix() + # This is only required when running on Windows with Python prior to 3.8: + utils.windows_event_loop_fix() enable_default_logger(log_file=args.log_file) loop = asyncio.get_event_loop() diff --git a/examples/utils.py b/examples/utils.py index f2d75e42e..ce08aad26 100644 --- a/examples/utils.py +++ b/examples/utils.py @@ -25,8 +25,11 @@ def build_parser(description: str): return parser -def asyncio_fix(): - if sys.platform == "win32" and sys.version_info[0] == 3 and sys.version_info[1] <= 7: +def windows_event_loop_fix(): + """Set up asyncio to use ProactorEventLoop implementation for new event loops on Windows.""" + + # For Python 3.8 ProactorEventLoop is already the default on Windows + if sys.platform == "win32" and sys.version_info < (3, 8): class _WindowsEventPolicy(asyncio.events.BaseDefaultEventLoopPolicy): _loop_factory = asyncio.windows_events.ProactorEventLoop diff --git a/examples/yacat/yacat.py b/examples/yacat/yacat.py index 1abeeb459..0bcd4315c 100644 --- a/examples/yacat/yacat.py +++ b/examples/yacat/yacat.py @@ -140,7 +140,8 @@ async def worker_find_password(ctx: WorkContext, tasks): args = parser.parse_args() - utils.asyncio_fix() + # This is only required when running on Windows with Python prior to 3.8: + utils.windows_event_loop_fix() enable_default_logger(log_file=args.log_file)