Created
October 11, 2020 19:58
-
-
Save Cologler/9eb709eb947759180d03b071d290ab06 to your computer and use it in GitHub Desktop.
setup process timeout
This file contains hidden or 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
def setup_proc_timeout(timeout: float, basedir: str, prefix: str=''): | |
''' | |
setup process timeout; | |
dump traceback from all threads to `basedir` if timeout. | |
''' | |
import os | |
import datetime | |
import atexit | |
import faulthandler | |
def _get_timestr(utc=False): | |
if utc: | |
dt = datetime.datetime.utcnow() | |
else: | |
dt = datetime.datetime.now() | |
dtstr = dt.isoformat()[:19] | |
return dtstr.replace(':', '-') | |
name = f'{prefix}{_get_timestr()}.log' | |
path = os.path.join(basedir, name) | |
os.makedirs(basedir, exist_ok=True) | |
fp = open(path, 'w') | |
faulthandler.dump_traceback_later(timeout, file=fp, exit=True) | |
@atexit.register | |
def _(): | |
fp.close() | |
os.unlink(path=fp.name) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment