2019-04-08 16:01:54 +00:00
|
|
|
import contextlib
|
2020-01-29 13:34:12 +00:00
|
|
|
import multiprocessing
|
2019-04-08 16:01:54 +00:00
|
|
|
import os
|
|
|
|
import shutil
|
2020-02-03 10:33:47 +00:00
|
|
|
import sys
|
2019-04-08 16:01:54 +00:00
|
|
|
import tempfile
|
2020-01-29 13:34:12 +00:00
|
|
|
import threading
|
2019-04-08 16:01:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
@contextlib.contextmanager
|
|
|
|
def temp_dir():
|
|
|
|
path = tempfile.mkdtemp(dir=os.environ.get('TEMP'))
|
|
|
|
try:
|
|
|
|
yield path
|
|
|
|
finally:
|
|
|
|
shutil.rmtree(path)
|
|
|
|
|
|
|
|
|
|
|
|
@contextlib.contextmanager
|
|
|
|
def autoremoved_file(path):
|
|
|
|
try:
|
|
|
|
with open(path, 'w') as handle:
|
|
|
|
yield handle
|
|
|
|
finally:
|
|
|
|
os.unlink(path)
|
2020-01-29 13:34:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
def run_function_in_parallel(func, args_list, threads=False):
|
|
|
|
processes = []
|
2020-02-03 10:33:47 +00:00
|
|
|
exit_code = 0
|
2020-01-29 13:34:12 +00:00
|
|
|
for task in args_list:
|
|
|
|
cls = threading.Thread if threads else multiprocessing.Process
|
|
|
|
processes.append(cls(target=func, args=task))
|
|
|
|
processes[-1].start()
|
|
|
|
for process in processes:
|
|
|
|
process.join()
|
2020-02-03 10:33:47 +00:00
|
|
|
if not threads:
|
|
|
|
if process.exitcode and not exit_code:
|
|
|
|
exit_code = process.exitcode
|
|
|
|
if exit_code:
|
|
|
|
sys.exit(exit_code)
|