@@ -316,6 +316,8 @@ class Reply:
316
316
"""Provide access to the result of a function execution that got dispatched
317
317
through WorkerPool.spawn()."""
318
318
319
+ _exception : BaseException | None = None
320
+
319
321
def __init__ (self , task , threadmodel : ExecModel ) -> None :
320
322
self .task = task
321
323
self ._result_ready = threadmodel .Event ()
@@ -328,10 +330,10 @@ def get(self, timeout: float | None = None):
328
330
including its traceback.
329
331
"""
330
332
self .waitfinish (timeout )
331
- try :
333
+ if self . _exception is None :
332
334
return self ._result
333
- except AttributeError :
334
- raise self ._exc from None
335
+ else :
336
+ raise self ._exception . with_traceback ( self . _exception . __traceback__ )
335
337
336
338
def waitfinish (self , timeout : float | None = None ) -> None :
337
339
if not self ._result_ready .wait (timeout ):
@@ -342,8 +344,9 @@ def run(self) -> None:
342
344
try :
343
345
try :
344
346
self ._result = func (* args , ** kwargs )
345
- except BaseException as exc :
346
- self ._exc = exc
347
+ except BaseException as e :
348
+ # sys may be already None when shutting down the interpreter
349
+ self ._exception = e
347
350
finally :
348
351
self ._result_ready .set ()
349
352
self .running = False
@@ -526,7 +529,9 @@ def __init__(self, outfile, infile, execmodel: ExecModel) -> None:
526
529
except (AttributeError , OSError ):
527
530
pass
528
531
self ._read = getattr (infile , "buffer" , infile ).read
529
- self ._write = getattr (outfile , "buffer" , outfile ).write
532
+ _outfile = getattr (outfile , "buffer" , outfile )
533
+ self ._write = _outfile .write
534
+ self ._flush = _outfile .flush
530
535
self .execmodel = execmodel
531
536
532
537
def read (self , numbytes : int ) -> bytes :
@@ -544,7 +549,7 @@ def write(self, data: bytes) -> None:
544
549
"""Write out all data bytes."""
545
550
assert isinstance (data , bytes )
546
551
self ._write (data )
547
- self .outfile . flush ()
552
+ self ._flush ()
548
553
549
554
def close_read (self ) -> None :
550
555
self .infile .close ()
0 commit comments