使用 futures.concurrent 和 class

using futures.concurrent with a class

我有一个如下所示的主要内容:

import gather_filings_per_filer
import os
from concurrent import futures

def put_last_i(arg):
    with open(os.path.dirname(os.path.abspath(__file__)) + '/last_i.txt','w') as f:
            f.write(arg)

def get_last_i():
    with open(os.path.dirname(os.path.abspath(__file__)) + '/last_i.txt','r') as f:
            data = f.readlines()
        return data[0]

if __name__=='__main__':
    i = int(get_last_i())
    g_f_p_f = gather_filings_per_filer.gather_filings_per()
    jobs=[]


    with futures.ThreadPoolExecutor(max_workers = 3) as executor:
        my_d = dict((executor.submit(g_f_p_f.mt_main),j) for j in range(i,297085))

        for future in futures.as_completed(my_d):
            print future
            print future.exception()

并且 g_f_p_f.mt_main 如下所示:

class gather_filings_per:
       def mt_main(self, i):
            print "Started:",i
            self.w_m = wrap_mysql.wrap_mysql()
            flag = True
            #do stuff...

给我以下结果:

<Future at 0x5dc2710 state=finished raised TypeError>
mt_main() takes exactly 2 arguments (1 given)
<Future at 0x5dc25b0 state=finished raised TypeError>
mt_main() takes exactly 2 arguments (1 given)

从我的角度来看,mt_main 只接受 1 个参数(考虑到典型的 self 行为,不需要 self)。

我遗漏了什么问题?

你是对的,除了隐含的 self,你只需要提供一个额外的参数。但是你什么都没给。所以你少了一个。拆分提交以使其在视觉上清晰:

my_d = dict(  # Making a dict
            # With key as result object from invoking `mt_main` with no additional arguments
            (executor.submit(g_f_p_f.mt_main),
            # And value j
             j)
            for j in range(i,297085))

也许您打算将 j 作为参数传递?假设它也应该是值,那就是:

# Note second argument to submit, which becomes only non-self argument to mt_main
my_d = dict((executor.submit(g_f_p_f.mt_main, j),j) for j in range(i,297085))

或者为了简化一个问题,因为 concurrent.futures 应该意味着你可以使用 dict comprehensions (这也将 submit 调用与其值 : 分开,以便更好视觉解析):

my_d = {executor.submit(g_f_p_f.mt_main, j): j for j in range(i, 297085)}