Quantcast

Examples with Stackless Python Re: InlineCallback Friendly ?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Examples with Stackless Python Re: InlineCallback Friendly ?

Andrew Francis
Hi Folks:

--- Andrew Francis <[hidden email]> wrote:

[J-P Calderone]
The only kind of dispatcher which is really hostile
towards inlineCallbacks is the kind which is
hostile towards Deferreds in general - ie, one which
requires a return value and does not support
Deferreds.

[A Francis]
Once again Jean-Paul, I find your explanation super
helpful and save me time going down the wrong path.


Starting off from where I left off in the "Adventures"
presentation, here are code snippets showing how to
handle other protocols with Stackless Python and
Twisted.

I am starting to experiment with resources as a part
of  prototyping REST support in WS-BPEL. I am also
interested in developing front-ends with Flex.

This is an example with resources adapted from the Abe
Fettig example on page 48 of "Twisted Network
Programming Essentials." I am still getting my feet
wet with resources

class HomePage(resource.Resource):
   
    def doWork(self):
        message = """
        <html>
        <head>
        </head>
        <body>
        Hello World    
        </body>
        </html>
        """
        self.request.write(message)
        self.request.finish()
       
       
    def render(self, request):
        self.request = request
        stackless.tasklet(self.doWork)()
        return server.NOT_DONE_YET


and here is one with PyAMF .2

(I remember sketching this one out on a napkin at
PyCon 2008)

class EchoServer(TwistedGateway):
    def __init__(self):
        super(EchoServer, self).__init__()
        self.request = None
        return
       
    def __echo__(self, request, deferred, y):
        print "=>", request, deferred, y
        deferred.callback(y)
       
   
    def echo(self, request, y):
        print "=>", request, y
        deferred = defer.Deferred()
        stackless.tasklet(self.__echo__)(request,
deferred, y)
        return deferred

Cheers,
Andrew





      ____________________________________________________________________________________
Be a better friend, newshound, and
know-it-all with Yahoo! Mobile.  Try it now.  http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ
from twisted.web              import resource, server
from twisted.internet         import reactor
from twisted.internet         import defer
from twisted.internet         import task
import stackless


class HomePage(resource.Resource):
   
    def doWork(self):
        message = """
        <html>
        <head>
        </head>
        <body>
        Hello World from Andrew    
        </body>
        </html>
        """
        self.request.write(message)
        self.request.finish()
       
       
    def render(self, request):
        self.request = request
        stackless.tasklet(self.doWork)()
        return server.NOT_DONE_YET
       
if __name__ == "__main__":
    root = resource.Resource()
    root.putChild('', HomePage())
    reactor.listenTCP(8000, server.Site(root))
    print "programme starting"
    task.LoopingCall(stackless.schedule).start(.01)
    stackless.tasklet(reactor.run)()
    stackless.run()
   
   
#!/usr/bin/env python
"""

"""
import stackless

from   twisted.web                                     import resource, http, server, error
from   twisted.internet                                import reactor
from   twisted.python                                  import log

from   pyamf                                           import remoting
from   pyamf.remoting.gateway.twisted                  import TwistedGateway
from   twisted.internet                                import defer
from   twisted.internet                                import task

class EchoServer(TwistedGateway):
    def __init__(self):
        super(EchoServer, self).__init__()
        self.request = None
        return
   
   
    def __echo__(self, request, deferred, y):
        print "=>", request, deferred, y
        deferred.callback(y)
       
   
    def echo(self, request, y):
        print "=>", request, y
        deferred = defer.Deferred()
        stackless.tasklet(self.__echo__)(request, deferred, y)
        return deferred
   
   
if __name__== "__main__":
    gw = EchoServer()
    gw.addService(gw.echo, "echo", "echo")
    root = resource.Resource()
    root.putChild('gwplayer', gw)
    reactor.listenTCP(8080, server.Site(root))
    print "reactor running"
    task.LoopingCall(stackless.schedule).start(.01)
    stackless.tasklet(reactor.run)()
    stackless.run()
   
   
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Examples with Stackless Python Re: InlineCallback Friendly ?

Thijs Triemstra
Hi Andrew,

thanks for the examples. Could you post this on our wiki in a page we can reference on http://pyamf.org/wiki/Examples? 
Cheers,

Thijs

On 18 apr 2008, at 17:26, Andrew Francis wrote:

Hi Folks:

--- Andrew Francis <[hidden email]> wrote:

[J-P Calderone]
The only kind of dispatcher which is really hostile
towards inlineCallbacks is the kind which is
hostile towards Deferreds in general - ie, one which
requires a return value and does not support
Deferreds.

[A Francis]
Once again Jean-Paul, I find your explanation super
helpful and save me time going down the wrong path.


Starting off from where I left off in the "Adventures"
presentation, here are code snippets showing how to
handle other protocols with Stackless Python and
Twisted.

I am starting to experiment with resources as a part
of  prototyping REST support in WS-BPEL. I am also
interested in developing front-ends with Flex.

This is an example with resources adapted from the Abe
Fettig example on page 48 of "Twisted Network
Programming Essentials." I am still getting my feet
wet with resources

class HomePage(resource.Resource):

   def doWork(self):
       message = """
       <html>
       <head>
       </head>
       <body>
       Hello World    
       </body>
       </html>
       """
       self.request.write(message)
       self.request.finish()


   def render(self, request):
       self.request = request
       stackless.tasklet(self.doWork)()
       return server.NOT_DONE_YET


and here is one with PyAMF .2

(I remember sketching this one out on a napkin at
PyCon 2008)

class EchoServer(TwistedGateway):
   def __init__(self):
       super(EchoServer, self).__init__()
       self.request = None
       return

   def __echo__(self, request, deferred, y):
       print "=>", request, deferred, y
       deferred.callback(y)


   def echo(self, request, y):
       print "=>", request, y
       deferred = defer.Deferred()
       stackless.tasklet(self.__echo__)(request,
deferred, y)
       return deferred

Cheers,
Andrew





     ____________________________________________________________________________________
Be a better friend, newshound, and
know-it-all with Yahoo! Mobile.  Try it now.  http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJfrom twisted.web              import resource, server
from twisted.internet         import reactor
from twisted.internet         import defer
from twisted.internet         import task
import stackless


class HomePage(resource.Resource):

   def doWork(self):
       message = """
       <html>
       <head>
       </head>
       <body>
       Hello World from Andrew    
       </body>
       </html>
       """
       self.request.write(message)
       self.request.finish()


   def render(self, request):
       self.request = request
       stackless.tasklet(self.doWork)()
       return server.NOT_DONE_YET

if __name__ == "__main__":
   root = resource.Resource()
   root.putChild('', HomePage())
   reactor.listenTCP(8000, server.Site(root))
   print "programme starting"
   task.LoopingCall(stackless.schedule).start(.01)
   stackless.tasklet(reactor.run)()
   stackless.run()

#!/usr/bin/env python
"""

"""
import stackless

from   twisted.web                                     import resource, http, server, error
from   twisted.internet                                import reactor
from   twisted.python                                  import log

from   pyamf                                           import remoting
from   pyamf.remoting.gateway.twisted                  import TwistedGateway
from   twisted.internet                                import defer
from   twisted.internet                                import task

class EchoServer(TwistedGateway):
   def __init__(self):
       super(EchoServer, self).__init__()
       self.request = None
       return


   def __echo__(self, request, deferred, y):
       print "=>", request, deferred, y
       deferred.callback(y)


   def echo(self, request, y):
       print "=>", request, y
       deferred = defer.Deferred()
       stackless.tasklet(self.__echo__)(request, deferred, y)
       return deferred


if __name__== "__main__":
   gw = EchoServer()
   gw.addService(gw.echo, "echo", "echo")
   root = resource.Resource()
   root.putChild('gwplayer', gw)
   reactor.listenTCP(8080, server.Site(root))
   print "reactor running"
   task.LoopingCall(stackless.schedule).start(.01)
   stackless.tasklet(reactor.run)()
   stackless.run()

_______________________________________________
PyAMF dev mailing list - [hidden email]
http://lists.pyamf.org/mailman/listinfo/dev


PGP.sig (194 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Examples with Stackless Python Re: InlineCallback Friendly ?

Andrew Francis
Hi Thijs:

--- Thijs Triemstra | Collab <[hidden email]> wrote:

> thanks for the examples. Could you post this on our
> wiki in a page we  can reference on
http://pyamf.org/wiki/Examples?

Give me a few days to flesh this out so users have
more context. I use this approach to prevent tasklets
from deadlocking the reactor while making certain
sequence of calls.

I also have client side examples.

Cheers,
Andrew


      ____________________________________________________________________________________
Be a better friend, newshound, and
know-it-all with Yahoo! Mobile.  Try it now.  http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Examples with Stackless Python Re: InlineCallback Friendly ?

Thijs Triemstra
Hi Andrew,

On 18 apr 2008, at 18:42, Andrew Francis wrote:

> Give me a few days to flesh this out so users have
> more context. I use this approach to prevent tasklets
> from deadlocking the reactor while making certain
> sequence of calls.
>
> I also have client side examples.

I went ahead and posted your first example on this page: http://pyamf.org/wiki/StacklessPythonTwisted

Cheers,

Thijs

PGP.sig (194 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Examples with Stackless Python Re: InlineCallback Friendly ?

Andrew Francis
Hi Thijs:

--- Thijs Triemstra | Collab <[hidden email]> wrote:

> I went ahead and posted your first example on this
> page: http://pyamf.org/wiki/StacklessPythonTwisted

No problem. However give me a few days so I can write
a more substantial example - for instance the same
example but also making an additional call to say a
XML-RPC backend. This where Stackless really comes in
handy.

So you get something like

#use Christopher Armstrong's blockOn technique
def blockOn(self, deferred):
    ch = stackless.channel()
    def cb(result):
        ch.send(result)
    deferred.addBoth(cb)
    return ch.receive()


def __echo__(self, request, deferred, y):
    result = blockOn(aProxy.callRemote(....)
    deferred.callback(result)

The pure Twisted example is more complex and way more
complex if you make more than one call - the callback
chain starts to become unruly.

Cheers,
Andrew




      ____________________________________________________________________________________
Be a better friend, newshound, and
know-it-all with Yahoo! Mobile.  Try it now.  http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ

Loading...