Created
November 16, 2013 20:40
-
-
Save zed/7504999 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python | |
"""Make zombie processes that are garbage collected.""" | |
import gc | |
import os | |
import time | |
from subprocess import Popen | |
def check_zombies(expected_nzombies): | |
#NOTE: don't use Popen() here | |
output = os.popen(r"ps aux | grep Z | grep -v grep").read() | |
print(output) | |
got_nzombies = len(output.splitlines()) - 1 | |
assert expected_nzombies == got_nzombies | |
check_zombies(0) # no zombies | |
# make zombies | |
n = 3 | |
for _ in range(n): | |
Popen("sleep 1", shell=True) # no references are kept | |
check_zombies(0) # no zombies | |
time.sleep(2) # wait for children to terminate and become zombies | |
check_zombies(n) # zombies. | |
unreachable_count = gc.collect() # collect garbage | |
assert unreachable_count == 0 | |
check_zombies(n) # zombies! | |
print("if you see it then subprocess doesn't reap children automatically.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment