Skip to content

Commit c2441f8

Browse files
committed
add .wait() to subprocess use in nistlat hoping to clean up occasional self-test failures
1 parent 14b3efe commit c2441f8

File tree

1 file changed

+75
-44
lines changed

1 file changed

+75
-44
lines changed

GSASII/nistlat.py

Lines changed: 75 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -145,25 +145,39 @@ def ReduceCell(center, cellin, mode=0, deltaV=0, output=None):
145145
if os.path.exists('NIST10'): # cleanup
146146
print("Removing old NIST10 file")
147147
os.remove('NIST10')
148-
p = subprocess.Popen([nistlattice],
148+
# import shutil
149+
# print(shutil.which(nistlattice))
150+
p = subprocess.Popen([nistlattice],encoding='UTF-8',
149151
stdin=subprocess.PIPE,
150152
stdout=subprocess.PIPE,
151153
stderr=subprocess.PIPE)
152-
p.stdin.write(bytearray(inp,'utf8'))
154+
p.stdin.write(inp)
153155
p.stdin.close()
154156
# read output and parse
155157
err = p.stderr.read()
158+
cellout = p.stdout.readlines()
159+
p.terminate()
160+
try:
161+
p.wait(1)
162+
except TimeoutExpired:
163+
print('timeout on wait')
164+
p.kill()
165+
try:
166+
p.wait(2)
167+
except TimeoutExpired:
168+
pass
169+
p.stdout.close()
170+
p.stderr.close()
171+
156172
celldict['input'] = (cellin,center,setting)
157173
celldict['output'] = []
158174
d = 1
159175
line = '?'
160-
linenum = 0
161176
fp = None
162177
if output: fp = open(output,'w')
163178
try:
164-
for b in p.stdout.readlines():
179+
for linenum,line in enumerate(cellout,1):
165180
linenum += 1
166-
line = b.decode()
167181
_emulateLP(line,fp)
168182
pat = r"T 2= (.*)/ (.*)/ (.*)" # transform matrix
169183
s = re.split(pat,line)
@@ -183,15 +197,11 @@ def ReduceCell(center, cellin, mode=0, deltaV=0, output=None):
183197
vol = float(re.split(r" *([\d\.-]*) *",s[2],maxsplit=1)[1])
184198
celldict['output'].append((d,lat,vol,mat,'P',' ')) # note reduced cells are all primitive
185199
except:
186-
print('ReduceCell parse error at line ',linenum)
187-
print(line)
200+
print(f'ReduceCell parse error at line {linenum}\n{line}')
188201
return celldict
189-
finally:
190-
p.terminate()
191-
p.stdout.close()
192-
p.stderr.close()
193202
if len(celldict['output']) == 0 or len(err) > 0:
194-
print('Error:' ,err.decode())
203+
print(f'ReduceCell Error = {err}')
204+
if output: fp.close()
195205
return celldict
196206

197207
def ConvCell(redcell):
@@ -212,30 +222,43 @@ def ConvCell(redcell):
212222
if os.path.exists('NIST10'): # cleanup
213223
print("Removing old NIST10 file")
214224
os.remove('NIST10')
215-
p = subprocess.Popen([convcell],
225+
p = subprocess.Popen([convcell],encoding='UTF-8',
216226
stdin=subprocess.PIPE,
217227
stdout=subprocess.PIPE,
218228
stderr=subprocess.PIPE)
219-
p.stdin.write(bytearray(inp,'utf8'))
229+
p.stdin.write(inp)
220230
p.stdin.close()
221231
# read output and parse
222232
err = p.stderr.read()
233+
out = p.stdout.readlines()
234+
p.terminate()
235+
try:
236+
p.wait(1)
237+
except TimeoutExpired:
238+
print('timeout on wait')
239+
p.kill()
240+
try:
241+
p.wait(2)
242+
except TimeoutExpired:
243+
pass
244+
p.stdout.close()
245+
p.stderr.close()
223246
if debug and err:
224-
print('ConvCell err=',err)
247+
print(f'ConvCell err = {err}')
225248
line = '?'
226249
linenum = 0
227250
cell = []
228251
center = ' '
229252
setting = ' '
230253
try:
231-
for b in p.stdout.readlines():
232-
line = b.decode()
254+
while out:
255+
line=out.pop(0)
256+
if not line.strip(): continue
257+
linenum += 1
233258
if '**WARNING**' in line:
234259
print('Note: Warning generated in conversion of reduced\n cell',
235260
redcell,'\n (Probably OK to ignore)')
236261
continue
237-
if not line.strip(): continue
238-
linenum += 1
239262
if linenum == 1:
240263
cell = [float(i) for i in line.split()[:6]]
241264
center = line.split()[-1]
@@ -245,20 +268,13 @@ def ConvCell(redcell):
245268
if linenum == 2:
246269
mat = np.array([float(i) for i in line.split()]).reshape(3,3)
247270
except:
248-
print('ConvCell parse error at line ',linenum)
249-
print(line)
250-
if debug:
251-
print("\nRemaining lines:")
252-
for b1 in p.stdout.readlines():
253-
print(b1.decode())
271+
print(f'ConvCell parse error at line {linenum}:\n{line}')
272+
if debug and out:
273+
print("\nUnprocessed convcell output:")
274+
for line in out: print(line)
254275
return None
255-
#return cell
256-
finally:
257-
p.terminate()
258-
p.stdout.close()
259-
p.stderr.close()
260276
if len(err) > 0:
261-
print('Error:' ,err.decode())
277+
print(f'ConvCell Error: {err}')
262278
return (cell,center,setting,mat)
263279

264280

@@ -312,19 +328,28 @@ def CompareCell(cell1, center1, cell2, center2, tolerance=3*[0.2]+3*[1],
312328
if os.path.exists('NIST10'): # cleanup
313329
print("Removing old NIST10 file")
314330
os.remove('NIST10')
315-
p = subprocess.Popen([nistlattice],
331+
p = subprocess.Popen([nistlattice],encoding='UTF-8',
316332
stdin=subprocess.PIPE,
317333
stdout=subprocess.PIPE,
318334
stderr=subprocess.PIPE)
319-
p.stdin.write(bytearray(inp,'utf8'))
335+
p.stdin.write(inp)
320336
p.stdin.close()
321337
err = p.stderr.read()
338+
lines = p.stdout.readlines()
322339
line = '?'
323340
fp = None
324341
if output: fp = open(output,'w')
325342
# read output and parse
326-
lines = [b.decode() for b in p.stdout.readlines()]
327343
p.terminate()
344+
try:
345+
p.wait(1)
346+
except TimeoutExpired:
347+
print('timeout on wait')
348+
p.kill()
349+
try:
350+
p.wait(2)
351+
except TimeoutExpired:
352+
pass
328353
p.stdout.close()
329354
p.stderr.close()
330355
if fp:
@@ -379,7 +404,7 @@ def CompareCell(cell1, center1, cell2, center2, tolerance=3*[0.2]+3*[1],
379404

380405
lnum += 1
381406
if len(err) > 0:
382-
print('Execution error:' ,err.decode())
407+
print(f'CompareCell error: {err}')
383408
return xforms
384409

385410
def CellSymSearch(cellin, center, tolerance=3*[0.2]+3*[1], mode=0,
@@ -435,20 +460,30 @@ def CellSymSearch(cellin, center, tolerance=3*[0.2]+3*[1], mode=0,
435460
if os.path.exists('NIST10'): # cleanup
436461
print("Removing old NIST10 file")
437462
os.remove('NIST10')
438-
p = subprocess.Popen([nistlattice],
463+
p = subprocess.Popen([nistlattice],encoding='UTF-8',
439464
stdin=subprocess.PIPE,
440465
stdout=subprocess.PIPE,
441466
stderr=subprocess.PIPE)
442-
p.stdin.write(bytearray(inp,'utf8'))
467+
p.stdin.write(inp)
443468
p.stdin.close()
444469
# read output and parse
445470
err = p.stderr.read()
471+
lines = p.stdout.readlines()
446472

447-
d = 1
448-
lines = [b.decode() for b in p.stdout.readlines()]
449473
p.terminate()
474+
try:
475+
p.wait(1)
476+
except TimeoutExpired:
477+
print('timeout on wait')
478+
p.kill()
479+
try:
480+
p.wait(2)
481+
except TimeoutExpired:
482+
pass
450483
p.stdout.close()
451484
p.stderr.close()
485+
486+
d = 1
452487
fp = None
453488
if output: fp = open(output,'w')
454489
if fp:
@@ -570,12 +605,8 @@ def CellSymSearch(cellin, center, tolerance=3*[0.2]+3*[1], mode=0,
570605
print('CellSymSearch parse error at line ',lnum,'\nNote error:',msg)
571606
print(line)
572607
return celldict
573-
# finally:
574-
# p.terminate()
575-
# p.stdout.close()
576-
# p.stderr.close()
577608
if len(symCellList) == 0 or len(err) > 0:
578-
print('Error:' ,err.decode())
609+
print(f'CellSymSearch error: {err}')
579610
return symCellList
580611

581612
if __name__ == '__main__': # test code

0 commit comments

Comments
 (0)