Public
Snippet $272 authored by Julien Muchembled

Load CPUs randomly (example of uses: increase probability of race conditions in NEO test suite, test power supplies...)

Edited
random_load.py
#!/usr/bin/python
from __future__ import division, print_function
import multiprocessing, os, subprocess, sys, threading, time

if len(sys.argv) > 1:
    w = None
else:
    r, w = os.pipe()
busy = []
tasks = []
try:
    for x in range(multiprocessing.cpu_count()):
        pid = os.fork()
        if pid:
            busy.append('.')
            tasks.append(pid)
        else:
            del tasks[:]
            from random import random
            w is None or os.close(r)
            x *= 2
            while 1:
                end = time.time() + random() / 2
                w is None or os.write(w, chr(x + 1))
                while time.time() < end:
                    random()
                w is None or os.write(w, chr(x))
                time.sleep(random() / 4)
    if w is None:
        sys.exit(subprocess.call(sys.argv[1:]))
    else:
        os.close(w)
        while 1:
            x = ord(os.read(r, 1))
            busy[x//2] = 'x' if x & 1 else '.'
            print(''.join(busy))
except KeyboardInterrupt:
    pass
finally:
    for pid in tasks:
        os.kill(pid, 9)