Skip to content

Instantly share code, notes, and snippets.

@tkf
Created September 6, 2018 05:41
Show Gist options
  • Save tkf/2dfceba5f1e8a98232e41c26525e613b to your computer and use it in GitHub Desktop.
Save tkf/2dfceba5f1e8a98232e41c26525e613b to your computer and use it in GitHub Desktop.
from contextlib import contextmanager
import os
import signal
import threading
import time
from julia.core import Julia
@contextmanager
def signal_handler(signalnum, handler):
"""
Context manager to run a code block with customised signal handler.
"""
orig_handler = signal.signal(signalnum, handler)
try:
yield
finally:
signal.signal(signalnum, orig_handler)
def test_sigint():
# https://github.com/JuliaPy/pyjulia/issues/189
julia = Julia()
def sigint_me():
ready.wait()
time.sleep(0.1)
for i in range(10):
print("Sending SIGINT i =", i)
time.sleep(0.01)
os.kill(os.getpid(), signal.SIGINT)
assert julia.eval("1") == 1
ready = threading.Event()
th = threading.Thread(target=sigint_me)
th.start()
with signal_handler(signal.SIGINT, signal.default_int_handler):
try:
ready.set()
time.sleep(1.0)
th.join(0.1)
except KeyboardInterrupt:
pass
else:
assert not th.is_alive()
raise AssertionError("SIGINT not received")
assert julia.eval("1") == 1
if __name__ == "__main__":
test_sigint()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment