Last active
May 1, 2021 15:43
-
-
Save lbolla/8576490 to your computer and use it in GitHub Desktop.
Killing zombies
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
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() | |
got_nzombies = len(output.splitlines()) | |
if got_nzombies > 0: | |
print 'You have zombies!' | |
print(output) | |
else: | |
print 'You are zombie-free!' | |
assert expected_nzombies == got_nzombies, '%d != %d' % ( | |
expected_nzombies, got_nzombies) | |
# Start with no zombies | |
check_zombies(0) | |
# Make zombies | |
n = 3 | |
for _ in xrange(n): | |
Popen("sleep 1", shell=True) | |
# No zombies so far | |
check_zombies(0) | |
# Wait for children to terminate and become zombies | |
print 'Sleep' | |
time.sleep(2) | |
# Zombies? | |
check_zombies(n) | |
# Yes! | |
raw_input('Paused') |
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
import os | |
import time | |
from subprocess import Popen | |
import gc | |
def check_zombies(expected_nzombies): | |
# NOTE: don't use Popen() here | |
output = os.popen(r"ps aux | grep ' Z' | grep -v grep").read() | |
got_nzombies = len(output.splitlines()) | |
if got_nzombies > 0: | |
print 'You have zombies!' | |
print(output) | |
else: | |
print 'You are zombie-free!' | |
assert expected_nzombies == got_nzombies, '%d != %d' % ( | |
expected_nzombies, got_nzombies) | |
# Start with no zombies | |
check_zombies(0) | |
# Make zombies | |
n = 3 | |
for _ in xrange(n): | |
Popen("sleep 1", shell=True) | |
# No zombies so far | |
check_zombies(0) | |
# Wait for children to terminate and become zombies | |
print 'Sleep' | |
time.sleep(2) | |
# Zombies? | |
check_zombies(n) | |
# Try GC | |
print 'GC' | |
gc.collect() | |
# Zombies? | |
check_zombies(n) | |
# Yes! | |
raw_input('Paused') |
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
import os | |
import time | |
from subprocess import Popen | |
import gc | |
def check_zombies(expected_nzombies): | |
# NOTE: don't use Popen() here | |
output = os.popen(r"ps aux | grep ' Z' | grep -v grep").read() | |
got_nzombies = len(output.splitlines()) | |
if got_nzombies > 0: | |
print 'You have zombies!' | |
print(output) | |
else: | |
print 'You are zombie-free!' | |
assert expected_nzombies == got_nzombies, '%d != %d' % ( | |
expected_nzombies, got_nzombies) | |
# Start with no zombies | |
check_zombies(0) | |
# Make zombies | |
n = 3 | |
procs = [] | |
for _ in xrange(n): | |
procs.append(Popen("sleep 1", shell=True)) | |
# No zombies so far | |
check_zombies(0) | |
# Wait for children to terminate and become zombies | |
print 'Sleep' | |
time.sleep(2) | |
# Zombies? | |
check_zombies(n) | |
# Explicitely del proc references | |
print 'Del procs references' | |
del procs | |
# Zombies? | |
check_zombies(0) | |
# No! | |
raw_input('Paused') |
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
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() | |
got_nzombies = len(output.splitlines()) | |
if got_nzombies > 0: | |
print 'You have zombies!' | |
print(output) | |
else: | |
print 'You are zombie-free!' | |
assert expected_nzombies == got_nzombies, '%d != %d' % ( | |
expected_nzombies, got_nzombies) | |
# Start with no zombies | |
check_zombies(0) | |
# Make zombies | |
n = 3 | |
for _ in xrange(n): | |
Popen("sleep 1", shell=True) | |
# No zombies so far | |
check_zombies(0) | |
# Wait for children to terminate and become zombies | |
print 'Sleep' | |
time.sleep(2) | |
# Zombies? | |
check_zombies(n) | |
# Sweep process table and remove zombies | |
for _ in xrange(n): | |
try: | |
print os.waitpid(-1, os.WNOHANG) | |
except Exception as exc: | |
print exc | |
break | |
# Zombies? | |
check_zombies(0) | |
# No! | |
raw_input('Paused') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment