Ví dụ về sử dụng concurrent.futures (backport cho 2.7):Bắt số dòng ban đầu cho ngoại lệ trong concurrent.futures
import concurrent.futures # line 01
def f(x): # line 02
return x * x # line 03
data = [1, 2, 3, None, 5] # line 04
with concurrent.futures.ThreadPoolExecutor(len(data)) as executor: # line 05
futures = [executor.submit(f, n) for n in data] # line 06
for future in futures: # line 07
print(future.result()) # line 08
Output:
1
4
9
Traceback (most recent call last):
File "C:\test.py", line 8, in <module>
print future.result() # line 08
File "C:\dev\Python27\lib\site-packages\futures-2.1.4-py2.7.egg\concurrent\futures\_base.py", line 397, in result
return self.__get_result()
File "C:\dev\Python27\lib\site-packages\futures-2.1.4-py2.7.egg\concurrent\futures\_base.py", line 356, in __get_result
raise self._exception
TypeError: unsupported operand type(s) for *: 'NoneType' and 'NoneType'
Chuỗi "...\_base.py", line 356, in __get_result"
không Endpoint tôi mong đợi để xem . Có thể nhận được dòng thực sự, nơi ngoại lệ đã được ném? Một cái gì đó như:
File "C:\test.py", line 3, in f
return x * x # line 03
Python3 có vẻ hiển thị số dòng chính xác trong trường hợp này. Tại sao không thể python2.7? Và có cách nào khác không?
Tôi cũng đang tìm kiếm một câu trả lời này câu hỏi. Cảm ơn! – drpoo