python異步回調函數的實現


#coding:utf-8 
from socket import *  
import time  
  
#簡單的服務器程序 監聽用戶連接,接收用戶發來的信息,並返回反饋  
def main():  
    HOST = ""  
    PORT = 3316  
    BUFSIZE = 1024  
    ADDR = (HOST, PORT)  
    tcpSerSock = socket(AF_INET, SOCK_STREAM)  
    tcpSerSock.bind(ADDR)  
    tcpSerSock.listen(5)  
    print "YoSQL bind port %d ,starting ...." % PORT  
    while  1:  
        print 'waiting for connection ...'  
        tcpCliSock, addr = tcpSerSock.accept()  
        print '...connected from:',addr  
  
        while 1:  
            try:  
                data = tcpCliSock.recv(BUFSIZE)  
                if not data:  
                    break  
                print 'data = %s' % data  
                i = data.find('[')  
                j = data.find(']')  
                if i!=-1 and j!=-1:  
                    sFuncId = data[i+1:j]  
                    message = data[j+1:]  
                    time.sleep(2)  
                    SendToListener("[%s] echo" % sFuncId)  
            except Exception, e:  
                print e  
                break  
        tcpCliSock.close()  
    tcpSerSock.close()  
  
def SendToListener(message):  
    listenerSock = socket(AF_INET, SOCK_STREAM)  
    listenerSock.connect(('localhost',7800))  
    listenerSock.send(message)  
    listenerSock.close()  
    print 'send to listener: %s' % message  
  
if __name__ == '__main__':  
    main()  
#http://blog.csdn.net/payinglee/article/details/9005010
#coding:utf-8
import threading  
import time  
from socket import *  
  
lCallback = {}  
iFuncId = 0   
  
#首先注冊函數,當接收到來自YoSQL的信息后,再調用該函數  
#callback機制:為某一事件注冊回調函數,當事件發生時,調用該函數  
def StartListener():  
    global iFuncId  
    global lCallback  
    HOST = ""  
    PORT = 7800  
    BUFSIZE = 1024  
    ADDR = (HOST, PORT)  
    tcpSerSock = socket(AF_INET, SOCK_STREAM)  
    tcpSerSock.bind(ADDR)  
    tcpSerSock.listen(5)  
    print "Listener bind port %d ,starting ...." % PORT  
    while 1:  
        print 'waiting for connection ...'  
        tcpCliSock, addr = tcpSerSock.accept()  
        print '...connected from:',addr  
        while 1:  
            try:  
                data = tcpCliSock.recv(BUFSIZE)  
                if not data:  
                    break  
                print 'data = %s' % data  
                i = data.find('[')  
                j = data.find(']')  
                if i!=-1 and j!=-1:  
                    iFuncId = int(data[i+1:j])  
                    message = data[j+1:]  
                    func = lCallback.get(iFuncId,None)  
                    if func:  
                        func()  
                        del lCallback[iFuncId]  
            except Exception,e:  
                print e  
                break  
        tcpCliSock.close()  
    tcpSerSock.close()  
  
  
def MyCallback():  
    print 'callback called !!!!!!!!!!'  
  
  
def Send(callback,message):  
    global iFuncId  
    global lCallback  
    lCallback[iFuncId] = callback  
    listenerSock = socket(AF_INET, SOCK_STREAM)  
    listenerSock.connect(('localhost',3316))  
    listenerSock.send("[%d] %s" % (iFuncId,message))  
    listenerSock.close()  
    iFuncId += 1  
    print 'send message to YoSQL : %s'%message  
  
def DoSomeThing():  
    print '......DoSomeThing......'  
if __name__ == '__main__':  
    t = threading.Thread(target=StartListener)  
    t.setDaemon(True)  
    t.start()  
    # t.join()  
    DoSomeThing()  
    DoSomeThing()  
    Send(MyCallback,"hahaha")  
    i = 0  
    while i < 20:  
        i+= 1  
        DoSomeThing()  
        try:  
            time.sleep(0.5)  
        except Exception,e:  
            print e  
            break  
    # t.join(2)  
    print '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>'  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM