Skip to content

Instantly share code, notes, and snippets.

@erm3nda
Last active January 19, 2017 14:39
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 erm3nda/ccb8c2b333aa34070a10 to your computer and use it in GitHub Desktop.
Save erm3nda/ccb8c2b333aa34070a10 to your computer and use it in GitHub Desktop.
That's how you limit CPU usage of a process using signal module
#!/usr/bin/env python
import time, os, sys, signal
sleeptime = waketime = 0.01
def send(signal, pids):
for pid in pids:
os.kill(pid, signal)
pids = map(int, sys.argv[1:])
while 1:
send(signal.SIGSTOP, pids)
try: time.sleep(sleeptime)
except KeyboardInterrupt, e:
send(signal.SIGCONT, pids)
raise e
send(signal.SIGCONT, pids)
time.sleep(waketime)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment