1848b8605Smrgimport sys 2848b8605Smrg 3848b8605Smrg# Monkey patch os.spawnve on windows to become thread safe 4848b8605Smrgif sys.platform == 'win32': 5848b8605Smrg import os 6848b8605Smrg import threading 7848b8605Smrg from os import spawnve as old_spawnve 8848b8605Smrg 9848b8605Smrg spawn_lock = threading.Lock() 10848b8605Smrg 11848b8605Smrg def new_spawnve(mode, file, args, env): 12848b8605Smrg spawn_lock.acquire() 13848b8605Smrg try: 14848b8605Smrg if mode == os.P_WAIT: 15848b8605Smrg ret = old_spawnve(os.P_NOWAIT, file, args, env) 16848b8605Smrg else: 17848b8605Smrg ret = old_spawnve(mode, file, args, env) 18848b8605Smrg finally: 19848b8605Smrg spawn_lock.release() 20848b8605Smrg if mode == os.P_WAIT: 21848b8605Smrg pid, status = os.waitpid(ret, 0) 22848b8605Smrg ret = status >> 8 23848b8605Smrg return ret 24848b8605Smrg 25848b8605Smrg os.spawnve = new_spawnve 26848b8605Smrg 27848b8605Smrg 28