Home  >  Article  >  Backend Development  >  python回调函数用法实例分析

python回调函数用法实例分析

WBOY
WBOYOriginal
2016-06-10 15:12:591815browse

本文实例讲述了python回调函数用法。分享给大家供大家参考。具体分析如下:

软件模块之间总是存在着一定的接口,从调用方式上,可以把他们分为三类:同步调用、回调和异步调用。同步调用是一种阻塞式调用,调用方要等待对方执行完毕 才返回,它是一种单向调用;回调是一种双向调用模式,也就是说,被调用方在接口被调用时也会调用对方的接口;异步调用是一种类似消息或事件的机制,不过它 的调用方向刚好相反,接口的服务在收到某种讯息或发生某种事件时,会主动通知客户方(即调用客户方的接口)。回调和异步调用的关系非常紧密,通常我们使用 回调来实现异步消息的注册,通过异步调用来实现消息的通知。同步调用是三者当中最简单的,而回调又常常是异步调用的基础,因此,这里我们着重讨论回调机制 在不同软件架构中的实现。

#call.py
import called
def callback():
  print "in callback"
def main():
  #called.test()
  called.test_call(callback)
  print "in call.py" 
main()

#called.py
'''''
def test():
  print "in called.py test()"
'''
def test_call(p_call):
  print "in called.py test_call()"
  p_call()

joe@joe:~/test/python$ python call.py
in called.py test_call()
in callback
in call.py
joe@joe:~/test/python$

希望本文所述对大家的Python程序设计有所帮助。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn