Sunday, 15 June 2014

python - Twisted: How is Deferred called from EndPoint when using pyglet-twisted -



python - Twisted: How is Deferred called from EndPoint when using pyglet-twisted -

the code below taken twisted's documentation on amp (link). when callback added d, there's automatically "protocol" argument added , deferred automatically run when reactor.run() called.

def connect(): endpoint = tcp4clientendpoint(reactor, "127.0.0.1", 8750) mill = factory() factory.protocol = amp homecoming endpoint.connect(factory) d = connect() def connected(protocol): homecoming protocol.callremote( registeruser, username=u'alice' d.addcallback(connected) reactor.run()

in code, same, except i've been using pyglet-twisted (link) cocos2d can't phone call reactor.run() because reactor starts @ same time application.

if phone call reactor.run(), error saying reactor running.

if don't, deferred doesn't seem called.

i've been trying phone call reactor.calllater, reactor.callwhenrunning, both need argument. passing in none doesn't work.

so question is, how should create this deferred run without calling reactor.run().

thanks!

few of twisted's apis succeed without running reactor. reactor responsible doing i/o. must have running reactor in order set connection (regardless of whether using endpoint object or other api so).

as far can tell, pyglet integration reactor not automatically start itself. must phone call run method. question suggests not calling it, i'm quite curious is calling it.

when modify illustration create finish , runnable , add together error reporting, this:

from pygletreactor import install install() twisted.internet import reactor twisted.internet.endpoints import tcp4clientendpoint twisted.internet.protocol import mill twisted.protocols.amp import amp twisted.python.log import err def connect(): endpoint = tcp4clientendpoint(reactor, "127.0.0.1", 8750) mill = factory() factory.protocol = amp homecoming endpoint.connect(factory) d = connect() def connected(protocol): homecoming protocol.callremote( registeruser, username=u'alice') d.addcallback(connected) d.adderrback(err) reactor.run()

then behavior expect, connection attempted , fail (because not running amp server anywhere):

unhandled error traceback (most recent phone call last): failure: twisted.internet.error.connectionrefusederror: connection refused other side: 111: connection refused.

perhaps can compare finish programme , find of import difference.

python twisted pyglet twisted.internet cocos2d-python

No comments:

Post a Comment