Python 執行緒開啟多工及回撥函式
阿新 • • 發佈:2018-12-19
# coding:utf-8 from time import sleep import thread
def long_io(cb): print "開始呼叫long_IO"
def func(callback): print "start long_IO" sleep(2) callback(1) print "end long_IO " thread.start_new_thread(func, (cb,)) print "long_IO函式返回"
def on_finish(ret): print "呼叫了回撥函式" print "ret %s" % ret print "結束了回撥函式"
def req_a(): print "start calling a" long_io(on_finish) print "end a"
def req_b(): print "start calling b" sleep(1) print "end b"
# 同步概念: if __name__ == '__main__': """模擬主迴圈排程""" req_a() print '-------------------' req_b() while True: pass