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

add support to run on top of trio #725

Closed
ZeeD opened this issue Jan 2, 2021 · 9 comments
Closed

add support to run on top of trio #725

ZeeD opened this issue Jan 2, 2021 · 9 comments
Labels
engine Chess engine integration

Comments

@ZeeD
Copy link

ZeeD commented Jan 2, 2021

the engine docs says

The preferred way to use the API is with an asyncio event loop.

is there some way to run python-chess on top of trio event loop?

@niklasf niklasf added the engine Chess engine integration label Jan 2, 2021
@niklasf
Copy link
Owner

niklasf commented Jan 2, 2021

Not directly, but the trio-asyncio compatibility layer looks promising.

I can get

import trio_asyncio
import chess.engine

async def main():
    transport, engine = await trio_asyncio.aio_as_trio(chess.engine.popen_uci)("/usr/bin/stockfish")

    result = await trio_asyncio.aio_as_trio(engine.play)(chess.Board(), chess.engine.Limit(time=0.1))
    print(result)

    await trio_asyncio.aio_as_trio(engine.quit)()

trio_asyncio.run(main)

to run with a small patch:

diff --git a/chess/engine.py b/chess/engine.py
index 2a02c7c..d970e9d 100644
--- a/chess/engine.py
+++ b/chess/engine.py
@@ -936,7 +936,7 @@ class Protocol(asyncio.SubprocessProtocol, metaclass=abc.ABCMeta):
     """Future: Exit code of the process."""

     def __init__(self: ProtocolT) -> None:
-        self.loop = asyncio.get_running_loop()
+        self.loop = asyncio.get_event_loop()
         self.transport: Optional[asyncio.SubprocessTransport] = None

         self.buffer = {
@@ -1195,7 +1195,7 @@ class Protocol(asyncio.SubprocessProtocol, metaclass=abc.ABCMeta):
                 popen_args["preexec_fn"] = os.setpgrp
         popen_args.update(kwargs)

-        return await asyncio.get_running_loop().subprocess_exec(cls, *command, **popen_args)  # type: ignore
+        return await asyncio.get_event_loop().subprocess_exec(cls, *command, **popen_args)  # type: ignore


 class CommandState(enum.Enum):

Somehow it does not appear to support asyncio.get_running_loop(), maybe due to python-trio/trio-asyncio#83?

@niklasf niklasf closed this as completed in 52797d3 Jan 2, 2021
@niklasf
Copy link
Owner

niklasf commented Jan 2, 2021

asyncio.get_running_loop() -> asyncio.events.get_running_loop() is a better workaround. Comitted that to master, so trio-asyncio is working with it, now.

@ZeeD
Copy link
Author

ZeeD commented Jan 5, 2021

Sorry, but I still have errors running your snippet:

I'm using python 3.8, trio 0.17.0, trio-asyncio 0.11.0 and chess 1.3.3 using setup.py with chess @ git+https://github.com/niklasf/python-chess#egg=chess as dependency

C:\Users\vito.detullio\Desktop\workspace-mystuff\moves_VENV\lib\site-packages\trio_asyncio\_base.py:20: TrioDeprecationWarning: trio.hazmat is deprecated since Trio 0.15.0; use trio.lowlevel instead (https://github.com/python-trio/trio/issues/476)
  from trio.hazmat import wait_for_child
Traceback (most recent call last):
  File "C:\Users\vito.detullio\Desktop\workspace-mystuff\moves\_dbg\demo.py", line 14, in <module>
    trio_asyncio.run(main)
  File "C:\Users\vito.detullio\Desktop\workspace-mystuff\moves_VENV\lib\site-packages\trio_asyncio\_loop.py", line 429, in run
    return trio.run(_run_task, proc, args)
  File "C:\Users\vito.detullio\Desktop\workspace-mystuff\moves_VENV\lib\site-packages\trio\_core\_run.py", line 1928, in run
    raise runner.main_task_outcome.error
  File "C:\Users\vito.detullio\Desktop\workspace-mystuff\moves_VENV\lib\site-packages\trio_asyncio\_loop.py", line 427, in _run_task
    return await proc(*args)
  File "C:\Users\vito.detullio\Desktop\workspace-mystuff\moves\_dbg\demo.py", line 7, in main
    transport, engine = await trio_asyncio.aio_as_trio(chess.engine.popen_uci)(moves.configurations.local.STOCKFISH)
  File "C:\Users\vito.detullio\Desktop\workspace-mystuff\moves_VENV\lib\site-packages\trio_asyncio\_adapter.py", line 56, in __call__
    return await self.loop.run_aio_coroutine(f)
  File "C:\Users\vito.detullio\Desktop\workspace-mystuff\moves_VENV\lib\site-packages\trio_asyncio\_base.py", line 240, in run_aio_coroutine
    return await run_aio_future(fut)
  File "C:\Users\vito.detullio\Desktop\workspace-mystuff\moves_VENV\lib\site-packages\trio_asyncio\_util.py", line 45, in run_aio_future
    res = await trio.hazmat.wait_task_rescheduled(abort_cb)
  File "C:\Users\vito.detullio\Desktop\workspace-mystuff\moves_VENV\lib\site-packages\trio\_core\_traps.py", line 166, in wait_task_rescheduled
    return (await _async_yield(WaitTaskRescheduled(abort_func))).unwrap()
  File "C:\Users\vito.detullio\Desktop\workspace-mystuff\moves_VENV\lib\site-packages\outcome\_impl.py", line 138, in unwrap
    raise captured_error
  File "C:\Users\vito.detullio\Desktop\workspace-mystuff\moves_VENV\lib\site-packages\trio_asyncio\_adapter.py", line 19, in _call_defer
    return await proc(*args, **kwargs)
  File "C:\Users\vito.detullio\Desktop\workspace-mystuff\moves_VENV\lib\site-packages\chess\engine.py", line 2573, in popen_uci
    transport, protocol = await UciProtocol.popen(command, setpgrp=setpgrp, **popen_args)
  File "C:\Users\vito.detullio\Desktop\workspace-mystuff\moves_VENV\lib\site-packages\chess\engine.py", line 1198, in popen
    return await asyncio.events.get_running_loop().subprocess_exec(cls, *command, **popen_args)  # type: ignore
  File "C:\Program Files\Python38\lib\asyncio\base_events.py", line 1615, in subprocess_exec
    transport = await self._make_subprocess_transport(
  File "C:\Program Files\Python38\lib\asyncio\base_events.py", line 487, in _make_subprocess_transport
    raise NotImplementedError
NotImplementedError

@niklasf niklasf reopened this Jan 5, 2021
@ZeeD
Copy link
Author

ZeeD commented Jan 5, 2021

Sorry to make you reopen the bug, I think the underlying cause is python-trio/trio-asyncio#97 (and the fact that I'm using a windows machine)

@PedanticHacker
Copy link
Contributor

Maybe wait until chess v1.3.4? Because chess v1.3.3 does not include the trio-asyncio fix that Niklas has commited to the master branch. Just my 2 cents.

But if you have manually edited your chess v1.3.3 codebase with the fix that Niklas has provided, then we have an unsolved issue.

@ZeeD
Copy link
Author

ZeeD commented Jan 5, 2021

@PedanticHacker I am using master:

chess @ git+https://github.com/niklasf/python-chess#egg=chess

FWIW the demo code seems to work on WSL

@PedanticHacker
Copy link
Contributor

This is clearly out of my expertise. Just wait some time, Niklas will respond.

@niklasf
Copy link
Owner

niklasf commented Jan 5, 2021

@PedanticHacker Have you received my email (end of December)? I was asking you to hold back on communication when you are unsure about an issue, to avoid needless notifications. Final warning.

@niklasf niklasf closed this as completed in 1053832 Jan 8, 2021
@niklasf
Copy link
Owner

niklasf commented Jan 8, 2021

Alright, trio-asyncio v0.12.0 (just released) includes the proper fix, so I reverted the workaround. Subprocess support is still missing on Windows, but there's not much that could be done in python-chess, so closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
engine Chess engine integration
Projects
None yet
Development

No branches or pull requests

3 participants