Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e90cedb

Browse files
committed
Issue #19425 -- a pickling error should not cause pool to hang.
1 parent 3797065 commit e90cedb

2 files changed

Lines changed: 19 additions & 5 deletions

File tree

Lib/multiprocessing/pool.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,8 @@ def __init__(self, processes=None, initializer=None, initargs=(),
147147

148148
self._task_handler = threading.Thread(
149149
target=Pool._handle_tasks,
150-
args=(self._taskqueue, self._quick_put, self._outqueue, self._pool)
150+
args=(self._taskqueue, self._quick_put, self._outqueue,
151+
self._pool, self._cache)
151152
)
152153
self._task_handler.daemon = True
153154
self._task_handler._state = RUN
@@ -338,7 +339,7 @@ def _handle_workers(pool):
338339
debug('worker handler exiting')
339340

340341
@staticmethod
341-
def _handle_tasks(taskqueue, put, outqueue, pool):
342+
def _handle_tasks(taskqueue, put, outqueue, pool, cache):
342343
thread = threading.current_thread()
343344

344345
for taskseq, set_length in iter(taskqueue.get, None):
@@ -349,9 +350,12 @@ def _handle_tasks(taskqueue, put, outqueue, pool):
349350
break
350351
try:
351352
put(task)
352-
except IOError:
353-
debug('could not put task on queue')
354-
break
353+
except Exception as e:
354+
job, ind = task[:2]
355+
try:
356+
cache[job]._set(ind, (False, e))
357+
except KeyError:
358+
pass
355359
else:
356360
if set_length:
357361
debug('doing set_length()')

Lib/test/test_multiprocessing.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1691,6 +1691,16 @@ def test_map_async_callbacks(self):
16911691
self.assertEqual(2, len(call_args))
16921692
self.assertIsInstance(call_args[1], ValueError)
16931693

1694+
def test_map_unplicklable(self):
1695+
# Issue #19425 -- failure to pickle should not cause a hang
1696+
if self.TYPE == 'threads':
1697+
return
1698+
class A(object):
1699+
def __reduce__(self):
1700+
raise RuntimeError('cannot pickle')
1701+
with self.assertRaises(RuntimeError):
1702+
self.pool.map(sqr, [A()]*10)
1703+
16941704
def test_map_chunksize(self):
16951705
try:
16961706
self.pool.map_async(sqr, [], chunksize=1).get(timeout=TIMEOUT1)

0 commit comments

Comments
 (0)