ThreadPoolExecutor executor.submit() returns 没有它就不会引发的异常

ThreadPoolExecutor executor.submit() returns an exception which is not raised without it

我注意到 ThreadPoolExecutorresult() 功能与我预期的不一样。它 returns 没有线程就不会引发异常。

这是一个可重现的最小示例:

import concurrent.futures
import time
class cat:
    def __init__(self, name=""):
        self.name = name
    def sayname(self):
        return("Miaw, " + self.name)

def main():
     names = ["Sprinkles", "Button", "Fluffy", "Semla"]
     cats = [cat(name = i) for i in names]
     with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
         cats_results = [executor.submit(i.sayname()) for i in cats]
     print([i.result for i in cats_results])

main()

Returns(注意 TypeError):

[<bound method Future.result of <Future at 0x7f227e3259d0 state=finished raised TypeError>>,
<bound method Future.result of <Future at 0x7f227e325c50 state=finished raised TypeError>>, 
<bound method Future.result of <Future at 0x7f227e325f50 state=finished raised TypeError>>, 
<bound method Future.result of <Future at 0x7f227e330290 state=finished raised TypeError>>, 
<bound method Future.result of <Future at 0x7f227e330590 state=finished raised TypeError>>]

这当然没有实际调用结果,但是如果我将 print([i.result for i in cats_results]) 更改为 print([i.result() for i in cats_results]) 它 returns:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 6, in main
  File "/usr/lib/python2.7/site-packages/concurrent/futures/_base.py", line 422, in result
    return self.__get_result()
  File "/usr/lib/python2.7/site-packages/concurrent/futures/thread.py", line 62, in run
    result = self.fn(*self.args, **self.kwargs)
TypeError: 'str' object is not callable

但如果我这样做:

def main():
     names = ["Sprinkles", "Button", "Fluffy", "Semla"]
     cats = [cat(name = i) for i in names]
     cats_results = [i.sayname() for i in cats]
     print([i for i in cats_results])

returns没问题:

['Miaw, Sprinkles', 'Miaw, Button', 'Miaw, Fluffy', 'Miaw, Semla']

知道 executor.submit 发生了什么事吗?

(我正在使用 python 3.6.6)

看起来像是打字错误,经过几次更正后一切正常:

import concurrent.futures
import time


class cat:
    def __init__(self, name=""):
        self.name = name

    def sayname(self):
        return ("Miaw, " + self.name)


def main():
    names = ["Sprinkles", "Button", "Fluffy", "Semla"]
    cats = [cat(name=i) for i in names]
    with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
        cats_results = [executor.submit(i.sayname) for i in cats]
    print([i.result() for i in cats_results])


main()