concurrent.futures.ThreadPoolExecutor 不打印错误
concurrent.futures.ThreadPoolExecutor doesn't print errors
我正在尝试将 concurrent.futures.ThreadPoolExecutor 模块与 运行 并行使用 class 方法,我的代码的简化版本大致如下:
class TestClass:
def __init__(self, secondsToSleepFor):
self.secondsToSleepFor = secondsToSleepFor
def testMethodToExecInParallel(self):
print("ThreadName: " + threading.currentThread().getName())
print(threading.currentThread().getName() + " is sleeping for " + str(self.secondsToSleepFor) + " seconds")
time.sleep(self.secondsToSleepFor)
print(threading.currentThread().getName() + " has finished!!")
with concurrent.futures.ThreadPoolExecutor(max_workers = 2) as executor:
futuresList = []
print("before try")
try:
testClass = TestClass(3)
future = executor.submit(testClass.testMethodToExecInParallel)
futuresList.append(future)
except Exception as exc:
print('Exception generated: %s' % exc)
如果我执行此代码,它的行为似乎符合预期。
但是如果我犯了一个错误,比如在 "testMethodToExecInParallel" 中指定了错误数量的参数,例如:
def testMethodToExecInParallel(self, secondsToSleepFor):
然后仍将函数提交为:
future = executor.submit(testClass.testMethodToExecInParallel)
或尝试在 "testMethodToExecInParallel" 方法的 print 语句中将字符串对象与整数对象连接(不使用 str(.) ):
def testMethodToExecInParallel(self):
print("ThreadName: " + threading.currentThread().getName())
print("self.secondsToSleepFor: " + self.secondsToSleepFor) <-- Should report an Error here
程序没有return任何错误;只打印 "before try" 并结束执行...
理解这使得程序几乎无法调试是微不足道的...有人能解释一下为什么会发生这种行为吗?
(对于第一种错误情况)concurrent.futures.ThreadPoolExecutor 不检查具有指定签名的函数以提交并最终抛出某种 "noSuchFunction" 异常?
可能在提交给 ThreadPoolExecutor class 方法而不是简单的独立函数时存在某种问题,因此,这种行为是可以预期的吗?
或者错误是在线程内部引发的,由于某种原因我无法阅读它?
-- 编辑 --
Akshay.N 建议在向 ThreadPoolExecutor 提交函数后插入 future.result() 使程序按预期运行:如果代码正确则运行良好,如果代码中的某些内容错误则打印错误.
我必须警告用户 ThreadPoolExecutor 的这种非常奇怪的行为:
如果你只提交函数给 ThreadPoolExecutor WITHOUT THEN CALLING future.result():
- 如果代码正确,程序继续运行并按预期运行
- 如果代码中有错误,似乎程序不会调用提交的函数,无论它做了什么:它不会报告代码中的错误
据我所知 "not so far",您必须在 "executor.submit(testClass.testMethodToExecInParallel)" 之后调用 "e.results()" 才能执行线程池。
我试过你说的但它给我错误,下面是代码
>>> import concurrent.futures as cf
>>> executor = cf.ThreadPoolExecutor(1)
>>> def a(x,y):
... print(x+y)
...
>>> future = executor.submit(a, 2, 35, 45)
>>> future.result()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\username
\AppData\Local\Programs\Python\Python37\lib\concurrent\futures\_base.py", line
425, in result
return self.__get_result()
File "C:\Users\username
\AppData\Local\Programs\Python\Python37\lib\concurrent\futures\_base.py", line
384, in __get_result
raise self._exception
File "C:\Users\username
\AppData\Local\Programs\Python\Python37\lib\concurrent\futures\thread.py", line
57, in run
result = self.fn(*self.args, **self.kwargs)
TypeError: a() takes 2 positional arguments but 3 were given
如果仍然无效请告诉我
我正在尝试将 concurrent.futures.ThreadPoolExecutor 模块与 运行 并行使用 class 方法,我的代码的简化版本大致如下:
class TestClass:
def __init__(self, secondsToSleepFor):
self.secondsToSleepFor = secondsToSleepFor
def testMethodToExecInParallel(self):
print("ThreadName: " + threading.currentThread().getName())
print(threading.currentThread().getName() + " is sleeping for " + str(self.secondsToSleepFor) + " seconds")
time.sleep(self.secondsToSleepFor)
print(threading.currentThread().getName() + " has finished!!")
with concurrent.futures.ThreadPoolExecutor(max_workers = 2) as executor:
futuresList = []
print("before try")
try:
testClass = TestClass(3)
future = executor.submit(testClass.testMethodToExecInParallel)
futuresList.append(future)
except Exception as exc:
print('Exception generated: %s' % exc)
如果我执行此代码,它的行为似乎符合预期。 但是如果我犯了一个错误,比如在 "testMethodToExecInParallel" 中指定了错误数量的参数,例如:
def testMethodToExecInParallel(self, secondsToSleepFor):
然后仍将函数提交为:
future = executor.submit(testClass.testMethodToExecInParallel)
或尝试在 "testMethodToExecInParallel" 方法的 print 语句中将字符串对象与整数对象连接(不使用 str(.) ):
def testMethodToExecInParallel(self):
print("ThreadName: " + threading.currentThread().getName())
print("self.secondsToSleepFor: " + self.secondsToSleepFor) <-- Should report an Error here
程序没有return任何错误;只打印 "before try" 并结束执行...
理解这使得程序几乎无法调试是微不足道的...有人能解释一下为什么会发生这种行为吗?
(对于第一种错误情况)concurrent.futures.ThreadPoolExecutor 不检查具有指定签名的函数以提交并最终抛出某种 "noSuchFunction" 异常?
可能在提交给 ThreadPoolExecutor class 方法而不是简单的独立函数时存在某种问题,因此,这种行为是可以预期的吗?
或者错误是在线程内部引发的,由于某种原因我无法阅读它?
-- 编辑 --
Akshay.N 建议在向 ThreadPoolExecutor 提交函数后插入 future.result() 使程序按预期运行:如果代码正确则运行良好,如果代码中的某些内容错误则打印错误.
我必须警告用户 ThreadPoolExecutor 的这种非常奇怪的行为: 如果你只提交函数给 ThreadPoolExecutor WITHOUT THEN CALLING future.result(): - 如果代码正确,程序继续运行并按预期运行 - 如果代码中有错误,似乎程序不会调用提交的函数,无论它做了什么:它不会报告代码中的错误
据我所知 "not so far",您必须在 "executor.submit(testClass.testMethodToExecInParallel)" 之后调用 "e.results()" 才能执行线程池。 我试过你说的但它给我错误,下面是代码
>>> import concurrent.futures as cf
>>> executor = cf.ThreadPoolExecutor(1)
>>> def a(x,y):
... print(x+y)
...
>>> future = executor.submit(a, 2, 35, 45)
>>> future.result()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\username
\AppData\Local\Programs\Python\Python37\lib\concurrent\futures\_base.py", line
425, in result
return self.__get_result()
File "C:\Users\username
\AppData\Local\Programs\Python\Python37\lib\concurrent\futures\_base.py", line
384, in __get_result
raise self._exception
File "C:\Users\username
\AppData\Local\Programs\Python\Python37\lib\concurrent\futures\thread.py", line
57, in run
result = self.fn(*self.args, **self.kwargs)
TypeError: a() takes 2 positional arguments but 3 were given
如果仍然无效请告诉我