Skip to content

Instantly share code, notes, and snippets.

@sdague
Created January 9, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sdague/c89c9ddedf8fb636592f to your computer and use it in GitHub Desktop.
Save sdague/c89c9ddedf8fb636592f to your computer and use it in GitHub Desktop.
#!/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