[Stackless] Twisted/Stackless UDP example Re: stackless socket, curiouser and curiouser

Andrew Francis andrewfr_ice at yahoo.com
Thu Mar 1 21:01:34 CET 2007


Message: 6
Date: Wed, 28 Feb 2007 18:26:53 +0000
From: "Richard Tew" <richard.m.tew at gmail.com>
Subject: Re: [Stackless] stackless socket, curiouser
and curiouser
>To: "Paul Sijben" <sijben at eemvalley.com>
Cc: stackless at stackless.com
Message-ID:
	
>If it is more important to you to just get things
>done, you might be better off to look at using a
>networking framework which is known to
>work with Stackless.  I do not have any personal
>knowledge of any which do so without some other form
>of shenanigans involved.  However there are some
>people on the list with experience mixing Stackless
>with Twisted who might wish to help out if you choose
>to go that direction.

Paul, I am not sure what you are trying to do but it
seems to involve setting up a UDP server. I feel
packages like asyncore simply require too much work to
do even for the simplest of programmes.

Here is a quick example I wrote based on the Twisted
Echo server example at
http://twistedmatrix.com/projects/core/documentation/howto/udp.html

The server listens at port 9999. When a connection
comes in, a tasklet prints out connection information.
I feel the solution is relatively easy to follow.

~~~~

#!/usr/bin/env python
"""
UDPServer.py
Andrew Francis
March 1st, 2007

Simple example of a UDP server using Twisted and
Stackless

Based on the Echo server example at
http://twistedmatrix.com/projects/core/documentation/howto/udp.html

<song>Rebellion - Arcade Fire</song>
"""


import stackless
from twisted.internet.protocol import DatagramProtocol
from twisted.internet import reactor

class Echo(DatagramProtocol):
    
    def __init__(self, requestChannel):
        self.channel = requestChannel
        return
    
    
    def datagramReceived(self, data, (host, port)):
        self.channel.send((data, host, port))
        self.transport.write(data, (host, port))


class EchoServer(object):
    def execute(self, port, requestChannel):
        reactor.listenUDP(port, Echo(requestChannel))
        reactor.run()


class EchoTasklet(object):
    def __init__(self, name, channel):
        self.channel = channel
        return

    def execute(self):
        while (1):
            data, host, port = self.channel.receive()
            print "received %r from %s:%d" % (data,
host, port)
            stackless.schedule()
            

if __name__ == "__main__":
    from twisted.internet import reactor
    
    """
    for simplicity - share the channel
    """
    requestChannel = stackless.channel()
    
    listener = EchoTasklet(requestChannel)
    server = EchoServer()
    
    stackless.tasklet(listener.execute)()
    stackless.tasklet(server.execute)(9999,
requestChannel)
    
    while (stackless.getruncount() > 1):
       stackless.schedule()

Cheers,
Andrew


 
____________________________________________________________________________________
The fish are biting. 
Get more visitors on your site using Yahoo! Search Marketing.
http://searchmarketing.yahoo.com/arp/sponsoredsearch_v2.php

_______________________________________________
Stackless mailing list
Stackless at stackless.com
http://www.stackless.com/mailman/listinfo/stackless



More information about the Stackless mailing list