#!/usr/bin/env python import os import threading import sys # Unless set to true this program will crash on Windows after a while if False: # Monkey patch os.spawnve on Windows to become thread safe from os import spawnve as old_spawnve spawn_lock = threading.Lock() def new_spawnve(mode, file, args, env): spawn_lock.acquire() try: if mode == os.P_WAIT: ret = old_spawnve(os.P_NOWAIT, file, args, env) else: ret = old_spawnve(mode, file, args, env) finally: spawn_lock.release() if mode == os.P_WAIT: pid, status = os.waitpid(ret, 0) ret = status >> 8 return ret os.spawnve = new_spawnve class SpawnThread(threading.Thread): def __init__(self, i): threading.Thread.__init__(self) self.file = r'C:\Windows\System32\cmd.exe' self.args = [self.file, '/C', '@exit'] self.env = {} if i == 0: for j in range(1024): self.env['A'*6 + hex(j)] = 'Y' * 256 def run(self): while True: os.spawnve(os.P_WAIT, self.file, self.args, self.env) def main(): threads = [SpawnThread(i) for i in range(8)] for t in threads: t.start() for t in threads: t.join() if __name__ == '__main__': main()