Twisted Python Network Programming learning framework (c), on defered

  In addition to the reactor reactor, Deferred Twisted probably most useful objects. You may use the Deferred several times in Twisted program, all necessary to understand how it works. Deferred may cause confusion in the beginning, but its purpose is simple: to keep track of asynchronous events, and the results obtained at the end of the event.
   Deferred be explained in this way: you might come across this problem in a restaurant, if you're waiting for your favorite table at the side hum ditty. Take a pager is a good idea, it can make you wait when there will not standing alone and bored. You can go for a walk at this time, next door to buy something. When a table is available, the pager rang, then you can go back to your hotel the location.
   This is similar to a Deferred pager. It provides a way to let the program find asynchronous task is completed, and at this time you can also do other things. When the function returns a Deferred object, note also take some time before the results obtained. In order to obtain results when the task is completed, you can specify an event handler for Deferred.

The easiest Deferred program:

from twisted.internet.defer import Deferred
def hello1(res):
    print(res)
    print('调用函数1')
def hello2(res):
    print(res)
    print('调用函数2')
d=Deferred()
d.addCallbacks(hello1,hello2)
d.callback('test')

d.addCallbacks (success, failure) when a successful call hello1, failed to call hello2.
Here Insert Picture Description

The following is a joined deferred client program.

from twisted.internet import reactor,defer,protocol 
class CallbackAndDisconnectProtocol(protocol.Protocol): 
    def connectionMade(self): 
        self.factory.deferred.callback("Connected!") 
        self.transport.loseConnection()
class ConnectionTestFactory(protocol.ClientFactory): 
    protocol=CallbackAndDisconnectProtocol 
    def __init__(self): 
        self.deferred=defer.Deferred() 
    def clientConnectionFailed(self,connector,reason): 
        self.deferred.errback(reason) 
def testConnect(host,port): 
    testFactory=ConnectionTestFactory() 
    reactor.connectTCP(host,port,testFactory) 
    return testFactory.deferred 
def handleSuccess(result,port): 
    print ("Connect to port %i"%port )
    reactor.stop() 
def handleFailure(failure,port): 
    print ("Error connecting to port %i: %s"%( port,failure.getErrorMessage())) 
    reactor.stop()
host='127.0.0.1'
port=51234
connecting=testConnect(host,port) 
connecting.addCallback(handleSuccess,port) 
connecting.addErrback(handleFailure,port) 
reactor.run()

When the execution reactor.stop () program will continue finished, a cycle is not executed. Deferred should be used in the future, need to learn.




Here are some resources to learn these days of Twisted:

https://blog.csdn.net/bluehawksky/article/details/79814577 some illustrative about the callback

http://blog.sina.com.cn/s/blog_704b6af70100py9n.html Twisted book chapters, based on a poetry project's github

https://www.cnblogs.com/zhangjing0502/archive/2012/05/17/2506687.html
explain to protrocl, factory, reactor in great detail

http://blog.sina.com.cn/s/blog_704b6af70100py9n.html sixty pages Baidu library of the most detailed explanation, Deep

Published 50 original articles · won praise 67 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_41033366/article/details/104200005