Last active
February 21, 2016 09:51
-
-
Save stakemori/067e254dc3d2054cf151 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
def start_cleaner_modified(): | |
""" | |
Start ``sage-cleaner`` in a new process group. | |
""" | |
pid = os.fork() | |
if not pid: | |
os.setpgid(os.getpid(), os.getpid()) | |
# Redirect stdin, stdout, stderr to /dev/null | |
with open(os.devnull, "r+") as f: | |
os.dup2(f.fileno(), 0) | |
os.dup2(f.fileno(), 1) | |
os.dup2(f.fileno(), 2) | |
# Close all other file descriptors | |
try: | |
maxopenfiles = os.sysconf("SC_OPEN_MAX") | |
if maxopenfiles <= 0: | |
raise ValueError | |
except ValueError: | |
maxopenfiles = 1024 | |
os.closerange(3, maxopenfiles) | |
os.execlp("sage-cleaner", "sage-cleaner") | |
else: | |
os.waitpid(pid, 0) | |
# Replace the original start_cleaner | |
sage.interfaces.cleaner.start_cleaner = start_cleaner_modified |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment