1import sys 2 3# Monkey patch os.spawnve on windows to become thread safe 4if sys.platform == 'win32': 5 import os 6 import threading 7 from os import spawnve as old_spawnve 8 9 spawn_lock = threading.Lock() 10 11 def new_spawnve(mode, file, args, env): 12 spawn_lock.acquire() 13 try: 14 if mode == os.P_WAIT: 15 ret = old_spawnve(os.P_NOWAIT, file, args, env) 16 else: 17 ret = old_spawnve(mode, file, args, env) 18 finally: 19 spawn_lock.release() 20 if mode == os.P_WAIT: 21 pid, status = os.waitpid(ret, 0) 22 ret = status >> 8 23 return ret 24 25 os.spawnve = new_spawnve 26 27 28