回调不会被调用 [英] Callback not getting called

查看:184
本文介绍了回调不会被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

代码,content = result.get()
如果代码== 0:
如果内容:
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没有被调用。在 apply_async 中指定回调函数时,

$ p $ <$> ,回调将在稍后的时间被调用。 'async'部分表示您可以在应用'作业完成之前,继续在当前线程上查看您的业务。



在你的示例代码中,你在循环中调用 apply_async ,但是你不等待任何操作完成。如果你想等待操作完成,你必须保持主线程(例如,通过阻塞或循环)。


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

When I see the value of new_content, it is empty and also callback function - done_callback is not getting called. Am I missing out on some part?

解决方案

When you specify a callback function in apply_async, the callback will be invoked at some later time. The 'async' part means you can continue going about your business on the current thread before the apply'd job has completed.

In your example code, you are calling apply_async in a loop but then you are not waiting for any of the operations to complete. You'll have to keep the main thread around (e.g., by blocking or looping) if you want to wait for the operations to complete.

这篇关于回调不会被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆