-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathhotwater-parallel.py
48 lines (39 loc) · 1.46 KB
/
hotwater-parallel.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import atexit
import glob
import os
import multiprocessing
import tecplot as tp
def init():
# !!! IMPORTANT !!!
# Must register stop at exit to ensure Tecplot cleans
# up all temporary files and does not create a core dump
atexit.register(tp.session.stop)
def work(datafile):
tp.new_layout()
tp.data.load_tecplot(datafile)
tp.active_frame().load_stylesheet('isosurface.sty')
imgfile = os.path.basename(datafile).replace('.plt', '.png')
tp.save_png(os.path.join('images', imgfile))
if __name__ == '__main__':
# !!! IMPORTANT !!!
# On Linux systems, Python's multiprocessing start method
# defaults to "fork" which is incompatible with PyTecplot
# and must be set to "spawn"
multiprocessing.set_start_method('spawn')
# Get the datafiles
files = glob.glob('hotwatermixing/HotWaterMixing_Y05YT10*.plt')
# Set up the pool with initializing function and associated arguments
num_workers = min(multiprocessing.cpu_count(), len(files))
pool = multiprocessing.Pool(num_workers, initializer=init)
try:
if not os.path.exists('images'):
os.makedirs('images')
# Map the work function to each of the job arguments
pool.map(work, files)
finally:
# !!! IMPORTANT !!!
# Must join the process pool before parent script exits
# to ensure Tecplot cleans up all temporary files
# and does not create a core dump
pool.close()
pool.join()