mirror of
https://github.com/wassname/catalyst.git
synced 2026-07-05 20:47:57 +08:00
Removes non-zipline specific delayed_signals module.
The delayed_signals module isn't actually necessary for the running of zipline, so removing to reduce the surface area.
This commit is contained in:
@@ -1,69 +0,0 @@
|
||||
#
|
||||
# Copyright 2012 Quantopian, Inc.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import os
|
||||
from signal import signal, SIGHUP, SIGINT
|
||||
import time
|
||||
from types import FrameType
|
||||
import unittest
|
||||
|
||||
from zipline.utils.delayed_signals import delayed_signals
|
||||
|
||||
|
||||
class DelayedSignals(unittest.TestCase):
|
||||
def handler(self, signum, frame):
|
||||
print "Got signal " + str(signum)
|
||||
self.got[signum] = time.time()
|
||||
self.assertTrue(isinstance(frame, FrameType))
|
||||
|
||||
def setUp(self):
|
||||
signal(SIGHUP, self.handler)
|
||||
signal(SIGINT, self.handler)
|
||||
|
||||
def reset(self):
|
||||
self.got = {}
|
||||
|
||||
def test_delayed_signals(self):
|
||||
self.reset()
|
||||
with delayed_signals([SIGHUP]):
|
||||
os.kill(os.getpid(), SIGHUP)
|
||||
time.sleep(2)
|
||||
self.assertTrue(self.got[SIGHUP])
|
||||
self.assertTrue(time.time() - self.got[SIGHUP] < 2)
|
||||
|
||||
def test_immediate_signals(self):
|
||||
self.reset()
|
||||
os.kill(os.getpid(), SIGHUP)
|
||||
time.sleep(2)
|
||||
self.assertTrue(self.got[SIGHUP])
|
||||
self.assertTrue(time.time() - self.got[SIGHUP] > 1)
|
||||
|
||||
def test_multiple_signals(self):
|
||||
self.reset()
|
||||
with delayed_signals([SIGHUP, SIGINT]):
|
||||
os.kill(os.getpid(), SIGINT)
|
||||
self.assertFalse(SIGHUP in self.got)
|
||||
self.assertTrue(SIGINT in self.got)
|
||||
|
||||
@delayed_signals([SIGHUP])
|
||||
def kill_and_sleep(self):
|
||||
os.kill(os.getpid(), SIGHUP)
|
||||
time.sleep(2)
|
||||
|
||||
def test_decorator(self):
|
||||
self.reset()
|
||||
self.kill_and_sleep()
|
||||
self.assertTrue(SIGHUP in self.got)
|
||||
self.assertTrue(time.time() - self.got[SIGHUP] < 2)
|
||||
@@ -1,41 +0,0 @@
|
||||
from functools import wraps
|
||||
from signal import signal
|
||||
|
||||
|
||||
class delayed_signals(object):
|
||||
"""
|
||||
Utility to temporary intercept one or more signals while a function or code
|
||||
block is executed, restore their signal handlers at the end of execution,
|
||||
and invoke them if the signals were in fact received during execution.
|
||||
|
||||
Can be used either as a decorator or a context manager.
|
||||
|
||||
Pass in an iterable of signals to intercept.
|
||||
"""
|
||||
|
||||
def handler(self, signum, frame=None):
|
||||
self.got.append({'signum': signum, 'frame': frame})
|
||||
|
||||
def __init__(self, signals):
|
||||
self.signals = signals
|
||||
self.handlers = {}
|
||||
self.got = []
|
||||
|
||||
def __enter__(self):
|
||||
for signum in self.signals:
|
||||
# signal() returns the old signal handler
|
||||
self.handlers[signum] = signal(signum, self.handler)
|
||||
|
||||
def __exit__(self, time, value, traceback):
|
||||
for signum, handler in self.handlers.items():
|
||||
signal(signum, handler)
|
||||
for signum, frame in ((i['signum'], i['frame']) for i in self.got):
|
||||
self.handlers[signum](signum, frame)
|
||||
|
||||
def __call__(self, fn):
|
||||
@wraps(fn)
|
||||
def call_fn(*args, **kwargs):
|
||||
with self:
|
||||
outval = fn(*args, **kwargs)
|
||||
return outval
|
||||
return call_fn
|
||||
Reference in New Issue
Block a user