audiokinetic / waapi-client-python

Decoupled autobahn WAMP client with support for plain options and bindable subscription callbacks
Apache License 2.0
41 stars 6 forks source link

On Windows, asyncio event loop gets stuck when calling from subprocess #1

Closed kakyoism closed 4 years ago

kakyoism commented 5 years ago

Thanks for the awesome waapi-client. It makes coding using WAAPI a breeze.

I recently bumped into this issue: when calling a WAAPI script through subprocess, the child process will never end.

I've fixed it locally by referring to this post: https://stackoverflow.com/questions/44633458/why-am-i-getting-notimplementederror-with-async-and-await-on-windows

For py3.7+, we only need a one-liner in client.py:

import platform

class WaapiClient(UnsubscribeHandler):
    def __init__(self, url=None, allow_exception=False):
        ...

        # ++++
        if platform.system() == 'Windows':
            asyncio.set_event_loop_policy(asyncio.WindowsProactorEventLoopPolicy())
        # ----

        self._loop = asyncio.get_event_loop()

        ...

Hope this helps!

SamuelLongchamps commented 5 years ago

Hi kakyoism, thanks for the feedback! Indeed the SelectorEventLoop provided as the default implementation on all platforms does not support subprocesses on Windows.

Since the default will be switched to ProactorEventLoop in 3.8, I think it's fair to backport this behavior to older Python versions.

I'll publish a patch release with this fix, thank you!

ghost commented 5 years ago

For those who use Python < 3.7. I tested on 3.6, but it should work on 3.5.

class WaapiClient(UnsubscribeHandler):
    def __init__(self, url=None, allow_exception=False):
        ...
        if sys.platform == 'win32':
            asyncio.set_event_loop(asyncio.ProactorEventLoop())
        self._loop = asyncio.get_event_loop()
        ...