Open davetapley opened 10 months ago
we're also hitting this but using multiprocessing.Pool
instead. Interestingly, setting reraise=True
avoids this issue and makes the code behave as expected.
Here is @davetapley's adapted MRE:
from multiprocessing import get_context
from dataclasses import dataclass
from typing import Callable, Generic, TypeVar
from tenacity import (retry, retry_if_exception_type, stop_after_attempt,
wait_fixed)
I = TypeVar('I')
O = TypeVar('O')
class IOException(Exception):
...
@dataclass
class Job(Generic[I, O]):
f: Callable[[I], O]
@retry(
retry=retry_if_exception_type(IOException),
wait=wait_fixed(0.1),
stop=stop_after_attempt(10),
#reraise=True,
)
def __call__(self, x: I):
return self.f(x)
def good_job(x: int) -> int:
return x * 2
def fail_job(x: int) -> int:
raise IOException('nope')
data = [1, 2, 3, 4, 5]
if __name__ == '__main__':
with get_context('spawn').Pool() as pool:
for result in pool.imap_unordered(Job(good_job), data):
print(result)
with get_context('spawn').Pool() as pool:
for result in pool.imap_unordered(Job(fail_job), data):
print(result)
output with reraise=False
:
multiprocessing.pool.MaybeEncodingError: Error sending result: '<multiprocessing.pool.ExceptionWithTraceback object at 0x1023aa320>'. Reason: 'TypeError("cannot pickle '_thread.RLock' object")'
output with reraise=True
:
__main__.IOException: nope
^ the issue also seems unrelated to the use of retry_if_exception_type
and wait_fixed
since removing those still causes the same error.
I also encountered this problem.
@spolloni's solution reraise=True
solves my problem. Thank you
Here's a weird one.
I'm using
ProcessPoolExecutor
and sometimes that causes in contention on my databases. I already usetenacity
elsewhere, so I figure can just wrap inretry_if_exception_type(IOException)
.But when
IOException
is thrown I getTypeError: cannot pickle '_thread.RLock' object
.MRE:
Output: