Hãy xem xét các tập tin sample.py
chứa đoạn mã sau:imap_unordered() treo lên nếu iterable ném một lỗi
from multiprocessing import Pool
def sample_worker(x):
print "sample_worker processes item", x
return x
def get_sample_sequence():
for i in xrange(2,30):
if i % 10 == 0:
raise Exception('That sequence is corrupted!')
yield i
if __name__ == "__main__":
pool = Pool(24)
try:
for x in pool.imap_unordered(sample_worker, get_sample_sequence()):
print "sample_worker returned value", x
except:
print "Outer exception caught!"
pool.close()
pool.join()
print "done"
Khi tôi thực hiện nó, tôi nhận được kết quả như sau:
Exception in thread Thread-2:
Traceback (most recent call last):
File "C:\Python27\lib\threading.py", line 810, in __bootstrap_inner
self.run()
File "C:\Python27\lib\threading.py", line 763, in run
self.__target(*self.__args, **self.__kwargs)
File "C:\Python27\lib\multiprocessing\pool.py", line 338, in _handle_tasks
for i, task in enumerate(taskseq):
File "C:\Python27\lib\multiprocessing\pool.py", line 278, in <genexpr>
self._taskqueue.put((((result._job, i, func, (x,), {})
File "C:\Users\renat-nasyrov\Desktop\sample.py", line 10, in get_sample_sequence
raise Exception('That sequence is corrupted!')
Exception: That sequence is corrupted!
Sau đó, ứng dụng bị treo. Làm thế nào tôi có thể xử lý tình huống mà không bị treo cổ?
Thụt lề của bạn sai… – tamasgal
@septi: đã được khắc phục, cảm ơn. – Pehat
Tôi bước qua mã với trình gỡ lỗi, và nó hoạt động như mong đợi. Tuy nhiên, khi tôi thực hiện nó tôi nhận được hành vi mà bạn mô tả. Vì vậy, có thể có một vấn đề đồng thời trong imap_unordered? – phobic