Created
January 9, 2015 14:20
-
-
Save sdague/c89c9ddedf8fb636592f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import logging | |
import subprocess | |
import threading | |
import time | |
from contextlib import contextmanager | |
FORMAT = '%(asctime)-15s %(message)s' | |
logging.basicConfig(format=FORMAT) | |
LOG = logging.getLogger('tcpserver') | |
@contextmanager | |
def watch(logger, action, level=logging.DEBUG, after=5.0): | |
start = time.time() | |
def log(): | |
msg = "%s timed out after %0.3fs" % (action, time.time() - start) | |
logger.log(level, msg) | |
timer = threading.Timer(after, log) | |
timer.start() | |
yield | |
timer.cancel() | |
with watch(LOG, "subprocess call", logging.ERROR): | |
subprocess.call("sleep 10", shell=True) | |
print "foo" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment