回调未被调用

时间:2015-09-28 01:53:25

标签: multithreading python-2.7 callback

for x,y in my_dict.iteritems():
     for z in y:
           def done_callback(result):

                code,content=result.get()
                if code==0:
                      if content:
                             new_content.append(content)
                      else:
                             pass
                else:
                      return error_html(environ,start_response,content)


           try:
                    pool.apply_async(function_returns_tuple,(x,z,my_val),done_callback)
           except Exception as e:
                    print e

当我看到new_content的值时,它是空的并且还有回调函数 - done_callback没有被调用。我错过了一些吗?

2 个答案:

答案 0 :(得分:0)

这是一个有用的最小例子:

from multiprocessing import Pool
from time import sleep

my_dict = { "a": [1, 2], "b": [3] }
my_val = 5

# has to be defined before Pool is created
def processing_function(args):
    (x, y, v) = args     # mapped function only gets one arg; so we unpack
    if y == 2:           # we throw a wrinkle in,
        return ('', '')  # to demonstrate filter
    return ("[Code %s %d %d]" % (x, y, v), "[Content %s %d %d]" % (x, y, v))


pool = Pool(2)    # execute two workers in parallel (feel free to change)

# make an iterator for all your values
inputs = ((x, z, my_val) for (x, y) in my_dict.iteritems() for z in y)
async_results = pool.map_async(processing_function, inputs)
pool.close()      # necessary before pool.join()
pool.join()       # wait for all processes in the pool to finish
results = async_results.get()    # now we can get the results
# we can extract just the results we want...
non_empty_content_joined = ''.join(
        content for (code, content) in results if content != '')
print non_empty_content_joined
# => [Content a 1 5][Content b 3 5]

答案 1 :(得分:0)

apply_async中指定回调函数时,稍后会调用回调函数。 ' async'部分意味着您可以在apply工作完成之前继续在当前线程上开展业务。

在您的示例代码中,您在循环中调用apply_async,但之后您不会等待任何操作完成。如果要等待操作完成,则必须保留主线程(例如,通过阻塞或循环)。