Skip to content

Instantly share code, notes, and snippets.

@zed
Created November 29, 2013 03:52
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/a16c3321ec8e5fbb8066 to your computer and use it in GitHub Desktop.
Save zed/a16c3321ec8e5fbb8066 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
"""Demonstrate that Ctrl-C (twice) interrupts a Python script with subprocess."""
import sys
from subprocess import Popen, PIPE
p = Popen([sys.executable or "python", "-uc", """
import time
n = int(input())
for i in range(n):
print("{:0{}d}".format(i, len(str(n))))
time.sleep(1)
"""], stdin=PIPE)
try:
p.communicate("300")
except KeyboardInterrupt:
print("got sigint")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment