Skip to content

Commit

Permalink
Fix process creation/destruction in Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
edyounis committed Dec 7, 2023
1 parent 45bb22e commit 2829453
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
11 changes: 9 additions & 2 deletions bqskit/compiler/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,11 @@ def _start_server(
params = f'{num_workers}, {runtime_log_level}, {worker_port=}'
import_str = 'from bqskit.runtime.attached import start_attached_server'
launch_str = f'{import_str}; start_attached_server({params})'
self.p = Popen([sys.executable, '-c', launch_str])
if sys.platform == 'win32':
flags = subprocess.CREATE_NEW_PROCESS_GROUP
else:
flags = 0
self.p = Popen([sys.executable, '-c', launch_str], creationflags=flags)
_logger.debug('Starting runtime server process.')

def _connect_to_server(self, ip: str, port: int) -> None:
Expand Down Expand Up @@ -183,7 +187,10 @@ def close(self) -> None:
# Shutdown server if attached
if self.p is not None and self.p.pid is not None:
try:
self.p.send_signal(signal.SIGINT)
if sys.platform == 'win32':
self.p.send_signal(signal.CTRL_C_EVENT)
else:
self.p.send_signal(signal.SIGINT)
_logger.debug('Interrupting attached runtime server.')
self.p.communicate(timeout=1)

Expand Down
2 changes: 2 additions & 0 deletions bqskit/runtime/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ def __init__(self) -> None:
# Safely and immediately exit on interrupt signals
handle = functools.partial(sigint_handler, node=self)
signal.signal(signal.SIGINT, handle)
if sys.platform == 'win32':
signal.signal(signal.CTRL_C_EVENT, handle)

# Start outgoing thread
self.outgoing: Queue[tuple[Connection, RuntimeMessage, Any]] = Queue()
Expand Down

0 comments on commit 2829453

Please sign in to comment.