0

I am getting: OSError: [Errno 24] Too many open files and stockfish is creating too many processes. How can I fix it?

This is the part of my code that makes the error:

import chess.engine

def stockfish(board, time_limit = 0.1):
    engine = chess.engine.SimpleEngine.popen_uci("stockfish/stockfish_10_x64")
    result = engine.analyse(board, chess.engine.Limit(time=time_limit, depth=0))
    score = str(result["score"])
    if "#" in score:
        return 999*int(abs(int(score[1:]))/int(score[1:]))
    else:
        return int(score)

def stockfish_from_fen(fen, time_limit = 0.1):
    import chess
    board = chess.Board(fen)
    return stockfish(board, time_limit)

while True:
    print(stockfish_from_fen("8/8/8/5k2/8/3K4/2Q5/8 b - - 0 1"))

A lot of "stockfish_10_x64" processes

TheLizzard
  • 7,248
  • 2
  • 11
  • 31
  • 3
    Looks like you're opening a file with `chess.engine.SimpleEngine.popen_uci("stockfish/stockfish_10_x64")` and then never closing it afterwards. I'm not familiar with this library, but maybe there's a `.close()` method or similar you can call on `engine` before the return in `stockfish()`? – Green Cloak Guy Oct 30 '19 at 17:30
  • I just has to use `with chess.engine.SimpleEngine.popen_uci("stockfish/stockfish_10_x64") as engine:` instead of `engine = chess.engine.SimpleEngine.popen_uci("stockfish/stockfish_10_x64")` – TheLizzard Oct 30 '19 at 17:34

0 Answers0