-
Notifications
You must be signed in to change notification settings - Fork 58
/
multiprocess_surfs.py
95 lines (67 loc) · 2.62 KB
/
multiprocess_surfs.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import concurrent.futures
import io
import logging
import xtgeo
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
TESTFILE = "../../xtgeo-testdata/surfaces/reek/1/basereek_rota.gri"
NTHREAD = 20
def _get_files_as_regularsurfaces_thread(option=1):
surfs = []
with concurrent.futures.ThreadPoolExecutor(max_workers=NTHREAD) as executor:
if option == 1:
futures = {executor.submit(_get_regsurff, i): i for i in range(NTHREAD)}
else:
futures = {executor.submit(_get_regsurfi, i): i for i in range(NTHREAD)}
for future in concurrent.futures.as_completed(futures):
try:
surf = future.result()
except Exception as exc:
logger.error("Error: %s", exc)
else:
surfs.append(surf)
return xtgeo.Surfaces(surfs)
def _get_files_as_regularsurfaces_multiprocess(option=1):
surfs = []
with concurrent.futures.ProcessPoolExecutor(max_workers=NTHREAD) as executor:
if option == 1:
futures = {executor.submit(_get_regsurff, i): i for i in range(NTHREAD)}
else:
futures = {executor.submit(_get_regsurfi, i): i for i in range(NTHREAD)}
for future in concurrent.futures.as_completed(futures):
try:
surf = future.result()
except Exception as exc:
logger.error("Error: %s", exc)
else:
surfs.append(surf)
return xtgeo.Surfaces(surfs)
def _get_regsurff(i):
logger.info("Start %s", i)
sfile = TESTFILE
logger.info("File is %s", sfile)
rf = xtgeo.surface_from_file(sfile, fformat="irap_binary", engine="python")
logger.info("End %s", i)
return rf
def _get_regsurfi(i):
logger.info("Start %s", i)
sfile = TESTFILE
with open(sfile, "rb") as fin:
stream = io.BytesIO(fin.read())
logger.info("File is %s", sfile)
rf = xtgeo.surface_from_file(stream, fformat="irap_binary")
logger.info("End %s", i)
return rf
if __name__ == "__main__":
SURFS1 = _get_files_as_regularsurfaces_thread(option=1)
for srf in SURFS1.surfaces:
logger.info("1 %s", srf.values.mean())
SURFS2 = _get_files_as_regularsurfaces_thread(option=2)
for srf in SURFS2.surfaces:
logger.info("2 %s", srf.values.mean())
SURFS3 = _get_files_as_regularsurfaces_multiprocess(option=1)
for srf in SURFS3.surfaces:
logger.info("3 %s", srf.values.mean())
SURFS4 = _get_files_as_regularsurfaces_multiprocess(option=2)
for srf in SURFS4.surfaces:
logger.info("4 %s", srf.values.mean())