使用concurrent.futures的示例(适用于2.7的后移版本):
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
输出:
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'
"...\_base.py", line 356, in __get_result"
这个字符串并不是我期望看到的端点。是否可能获取抛出异常的真实行数?例如:
File "C:\test.py", line 3, in f
return x * x # line 03
Python3 在这种情况下似乎可以显示正确的行号,为什么 Python2.7 不能呢?是否有任何解决方法?