Skip to content

Commit bdb7140

Browse files
committed
Officially support setpgrp (niklasf#123)
1 parent 69592f5 commit bdb7140

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

chess/uci.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1373,7 +1373,7 @@ def is_alive(self):
13731373
return self.process.is_alive()
13741374

13751375

1376-
def popen_engine(command, engine_cls=Engine, _popen_lock=threading.Lock()):
1376+
def popen_engine(command, engine_cls=Engine, setpgrp=False, _popen_lock=threading.Lock()):
13771377
"""
13781378
Opens a local chess engine process.
13791379
@@ -1387,15 +1387,25 @@ def popen_engine(command, engine_cls=Engine, _popen_lock=threading.Lock()):
13871387
>>> engine.author
13881388
'Tord Romstad, Marco Costalba and Joona Kiiski'
13891389
1390-
The input and input streams will be linebuffered and able both Windows
1391-
and Unix newlines.
1390+
:param setpgrp: Open the engine process in a new process group. This will
1391+
stop signals (such as keyboards interrupts) from propagating from the
1392+
parent process. Defaults to ``False``.
13921393
"""
13931394
engine = engine_cls()
13941395

1396+
kwargs = {}
1397+
if setpgrp:
1398+
try:
1399+
# Windows
1400+
kwargs["creationflags"] = subprocess.CREATE_NEW_PROCESS_GROUP
1401+
except AttributeError:
1402+
# Unix
1403+
kwargs["preexec_fn"] = os.setpgrp
1404+
13951405
# Work around possible race condition in Python 2 subprocess module,
13961406
# that can occur when concurrently opening processes.
13971407
with _popen_lock:
1398-
PopenProcess(engine, command)
1408+
PopenProcess(engine, command, **kwargs)
13991409

14001410
return engine
14011411

0 commit comments

Comments
 (0)