thomasahle / sunfish

Sunfish: a Python Chess Engine in 111 lines of code
https://www.chessprogramming.org/Sunfish
Other
2.95k stars 543 forks source link

How much of the UCI protocol is implemented? #93

Closed jxu closed 1 year ago

jxu commented 2 years ago

Is it documented anywhere? I was thinking about writing an engine myself and wondering how I could implement UCI stateless protocol, including stop / go infinite. I thought I either needed threads or interrupt execution every so often to check if it should stop.

But from uci.py I don't see stop or go infinite implemented anywhere.

thomasahle commented 1 year ago

You'll probably need something you monitor every once in a while in your search function. But you need that anyway if you have a way to stop based on timeout. What do you think about something like this?

from concurrent.futures import ThreadPoolExecutor
from threading import Event
import time, itertools

def go_infinite(event):
    for i in itertools.count():
        if event.is_set():
            break
    return i

with ThreadPoolExecutor(max_workers=1) as executor:
    stop = Event()
    future = executor.submit(go_infinite, stop)

    while True:
        cmd = input('Say stop: ')
        print('You said', cmd)
        if cmd == 'stop':
            stop.set()
            break

    print('Result:', future.result())

You can also check this: https://superfastpython.com/stop-a-thread-in-python/