Why am I getting NotImplementedError with async and await on Windows?
Different event loops are implemented differently. Some of them have restrictions (sometimes OS-related). By default, Windows uses SelectorEventLoop and as you can see in doc:
SelectorEventLoop has the following limitations:
- SelectSelector is used to wait on socket events: it supports sockets and is limited to 512 sockets.
- loop.add_reader() and loop.add_writer() only accept socket handles (e.g. pipe file descriptors are not supported).
- Pipes are not supported, so the loop.connect_read_pipe() and loop.connect_write_pipe() methods are not implemented.
- Subprocesses are not supported, i.e. loop.subprocess_exec() and loop.subprocess_shell() methods are not implemented.
To run your code in Windows you can use alternative event loop available by default - ProactorEventLoop
.
Replace line:
loop = asyncio.get_event_loop()
with this:
loop = asyncio.ProactorEventLoop()
asyncio.set_event_loop(loop)
Your code will work.
3.7.0 Python documentation handles this here: https://docs.python.org/3/library/asyncio-platforms.html#asyncio-windows-subprocess
Set the event loop policy if you are using Windows - then your code will work.
In your startup, change the unix-specific section:
cmds = [
['du', '-sh', '/Users/fredrik/Desktop'],
['du', '-sh', '/Users/fredrik'],
['du', '-sh', '/Users/fredrik/Pictures']
]
to handle Windows & Unix:
if 'win32' in sys.platform:
# Windows specific event-loop policy & cmd
asyncio.set_event_loop_policy(asyncio.WindowsProactorEventLoopPolicy())
cmds = [['C:/Windows/system32/HOSTNAME.EXE']]
else:
# Unix default event-loop policy & cmds
cmds = [
['du', '-sh', '/Users/fredrik/Desktop'],
['du', '-sh', '/Users/fredrik'],
['du', '-sh', '/Users/fredrik/Pictures']
]