Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

provide a fix for asyncio on Windows + Python pre-3.8 #105

Merged
merged 15 commits into from
Nov 16, 2020
Merged
3 changes: 2 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
python-version: [3.6, 3.7, 3.8]
python-version: [3.6, 3.7, 3.8, 3.9]
os:
- ubuntu-latest
- macos-latest
- windows-latest
fail-fast: false
if: matrix.os != 'windows-latest' || matrix.python-version != '3.9'

steps:
- uses: actions/checkout@v2
Expand Down
10 changes: 7 additions & 3 deletions examples/blender/blender.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import pathlib
import sys

from yapapi import Executor, Task, __version__ as yapapi_version, WorkContext
from yapapi import Executor, Task, __version__ as yapapi_version, WorkContext, asyncio_fix
from yapapi.log import enable_default_logger, log_summary, log_event_repr # noqa
from yapapi.package import vm
from datetime import timedelta
Expand Down Expand Up @@ -85,17 +85,21 @@ async def worker(ctx: WorkContext, tasks):
parser.set_defaults(log_file="blender-yapapi.log")
args = parser.parse_args()

asyncio_fix()
shadeofblue marked this conversation as resolved.
Show resolved Hide resolved

enable_default_logger(log_file=args.log_file)

loop = asyncio.get_event_loop()

subnet = args.subnet_tag
sys.stderr.write(
f"yapapi version: {utils.TEXT_COLOR_YELLOW}{yapapi_version}{utils.TEXT_COLOR_DEFAULT}\n"
)
sys.stderr.write(f"Using subnet: {utils.TEXT_COLOR_YELLOW}{subnet}{utils.TEXT_COLOR_DEFAULT}\n")
task = loop.create_task(main(subnet_tag=args.subnet_tag))
try:
asyncio.get_event_loop().run_until_complete(task)
loop.run_until_complete(task)
except (Exception, KeyboardInterrupt) as e:
print(e)
task.cancel()
asyncio.get_event_loop().run_until_complete(task)
loop.run_until_complete(task)
4 changes: 3 additions & 1 deletion examples/yacat/yacat.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import pathlib
import sys

from yapapi import Executor, Task, WorkContext
from yapapi import Executor, Task, WorkContext, asyncio_fix
from yapapi.log import enable_default_logger, log_summary, log_event_repr # noqa
from yapapi.package import vm

Expand Down Expand Up @@ -141,6 +141,8 @@ async def worker_find_password(ctx: WorkContext, tasks):

args = parser.parse_args()

asyncio_fix()

enable_default_logger(log_file=args.log_file)

sys.stderr.write(
Expand Down
12 changes: 12 additions & 0 deletions tests/test_yapapi.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import asyncio
import toml
from pathlib import Path

Expand All @@ -9,3 +10,14 @@ def test_version():
pyproject = toml.loads(f.read())

assert yapapi.__version__ == pyproject["tool"]["poetry"]["version"]


def test_asyncio_fix():
async def _asyncio_test():
await asyncio.create_subprocess_shell("")

yapapi.asyncio_fix()

l = asyncio.get_event_loop()
t = l.create_task(_asyncio_test())
l.run_until_complete(t)
11 changes: 11 additions & 0 deletions yapapi/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""Golem Python API."""
import asyncio
import sys
import toml

from pathlib import Path
Expand All @@ -21,5 +23,14 @@ def get_version() -> str:
return get_distribution("yapapi").version


def asyncio_fix():
if sys.platform == "win32":

class _WindowsEventPolicy(asyncio.events.BaseDefaultEventLoopPolicy):
_loop_factory = asyncio.windows_events.ProactorEventLoop

asyncio.set_event_loop_policy(_WindowsEventPolicy())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd just move those statements to the module level, outside any function

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

then I'd need more "if sys.platform" etc since this class is not valid under non-Windows platforms ...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, unless you mean the same as above - then, again, I'm unsure if this is a good idea to force that upon people ...



__version__: str = get_version()
__all__ = ["Executor", "props", "rest", "executor", "storage", "Task", "WorkContext"]