Skip to content

Instantly share code, notes, and snippets.

@zed
Created November 16, 2013 21:11
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 zed/7505372 to your computer and use it in GitHub Desktop.
Save zed/7505372 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
"""Make zombies using `.wait()`.
http://stackoverflow.com/questions/2760652
"""
import os
from threading import Timer
from subprocess import Popen
n = 10
many_seconds = 10
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
processes = [Popen("sleep %d" % many_seconds, shell=True)]
processes.extend(Popen("sleep 1", shell=True) for _ in range(n))
Timer(2, check_zombies, [n]).start() # should see zombies in 2 seconds
for p in processes:
p.wait()
check_zombies(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment