Skip to content

Commit

Permalink
feat: Handle control-c with less noise
Browse files Browse the repository at this point in the history
Interrupting sampling makes much less noise now as KeyboardInterrupt
is ignored by the worker processes. Commit also avoids
raising an additional exception when a KeyboardInterrupt stops
sampling.

Stopping sampling via control-c occurs in PyStan. This change
should not affect anyone using httpstan by itself.
  • Loading branch information
riddell-stan committed Apr 26, 2021
1 parent a1df6f3 commit 8d077fe
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
8 changes: 7 additions & 1 deletion httpstan/services_stub.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import multiprocessing as mp
import os
import select
import signal
import socket
import tempfile
import typing
Expand All @@ -26,9 +27,14 @@
import httpstan.services.arguments as arguments
from httpstan.config import HTTPSTAN_DEBUG


# Use `get_context` to get a package-specific multiprocessing context.
# See "Contexts and start methods" in the `multiprocessing` docs for details.
executor = concurrent.futures.ProcessPoolExecutor(mp_context=mp.get_context("fork"))
def init_worker() -> None:
signal.signal(signal.SIGINT, signal.SIG_IGN) # ignore KeyboardInterrupt


executor = concurrent.futures.ProcessPoolExecutor(mp_context=mp.get_context("fork"), initializer=init_worker)
logger = logging.getLogger("httpstan")


Expand Down
6 changes: 5 additions & 1 deletion httpstan/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,11 @@ def _services_call_done(operation: dict, future: asyncio.Future) -> None:
operation["result"] = _make_error(message, status=status)
# Delete messages associated with the fit. If initialization
# fails, for example, messages will exist on disk. Remove them.
httpstan.cache.delete_fit(operation["metadata"]["fit"]["name"])
try:
httpstan.cache.delete_fit(operation["metadata"]["fit"]["name"])
except FileNotFoundError:
# occurs when control-c stops sampling
pass
else:
logger.info(f"Operation `{operation['name']}` finished.")
operation["result"] = schemas.Fit().load(operation["metadata"]["fit"])
Expand Down

0 comments on commit 8d077fe

Please sign in to comment.