Hi Lauren:

Message: 1
Date: Thu, 21 Oct 2010 23:47:56 +0200
From: Laurens Van Houtven <[email protected]>
Subject: [Twisted-Python] PyCon extreme Twisted talk!
To: Twisted general discussion <[email protected]>
Message-ID:
    <[email protected]>
Content-Type: text/plain; charset="utf-8"

>Greetings fellow Twisted aficionados!


>So, Allen (dash) and I have decided that we were going to co-do the >Twisted 
>Extreme talk at PyCon US 2011. Extreme talks are like talks, >except with less 
>talking and more coding, so preferably something >actionable for an entire 45 
>minutes! That most likely means many steps: >start small, and then do 
>something more complex.

I am probably going to submit an extreme talk for PyCon 2011. I'll either 
refine the "Prototyping Go's Select" talk. Or discuss about my work-in-progress 
about join conditions. Twisted has a simple join in the form of a deferred list.

Inspired by the Ray Hettinger Monocle post-talk discussion and writing examples 
with stackless.select(), I thought submitting another extreme talk that would 
revisit the lost world of Stackless Python/Twisted integration. As we 
discussed, why not a special reactor that incorporates the Stackless scheduler? 
Right now, a lot my examples use Twisted for sleep(). Creating a Stackless 
reactor would hopefully allow me to get rid of stuff like

This way I can get rid of stuff like:

l = task.LoopingCall(stackless.schedule)
l.start(.001)
stackless.tasklet(startTwisted)()
stackless.tasklet(santa)(reindeers, elves)
stackless.run()

and eventually 

def tick(seconds):
    tickCh = stackless.channel()
    reactor.callLater(seconds, tickCh.send, None)
    tickCh.receive()

what is particularly exciting is talk in the pypy-dev list about incorporating 
Stackless with the JIT....

Cheers,
Andrew

P.S - I have provided an example of using Stackless and Twisted to solve the 
Santa Claus Problem


      
#!/usr/bin/env python
"""
simpleSanta.py
Andrew Francis
October 21th, 2010

The purpose of simpleSanta is as follows:

1) Show a solution to the Santa Claus problem using the select
2) Point out improvements to the select statement
3) Give hints as to why a solution with Join conditions may be easier

for now, we leave out priority to reindeer

"""

import random

import time
import sys

from twisted.internet                                 import reactor
from twisted.internet                                 import defer
from twisted.internet                                 import task
from twisted.python.failure                           import Failure

import stackless

# we can use ETIME and RTIME to control how long reindeers and elves wait
# before asking questions

RTIME = (10,12)
ETIME = (1,12)


def tick(seconds):
    tickCh = stackless.channel()
    reactor.callLater(seconds, tickCh.send, None)
    tickCh.receive()


def startTwisted():
    reactor.run()


def stopTwisted():
    reactor.callLater(1, reactor.stop)
    print "that's all folks"


def worker(ch, name, theRange):
    myChannel = stackless.channel()
    while True:
       waitTime = random.randint(theRange[0], theRange[1])
       tick(waitTime)
       ch.send((name, myChannel))
       answer = myChannel.receive()


"""
a way of telling the tasklets that Santa is done
"""
def acceptChannels(channels, status):
    for name, channel in channels:
        channel.send(status)


def deliveryToys(reindeer):
    print "All the reindeer have arrived - delivering toys"
    acceptChannels(reindeer, True)


def consultWithSanta(elves):
    print "Santa consulting with elves"
    acceptChannels(elves, True)


def santa(reindeer, elves):
    reindeerChannels = [ch for name, ch, waitTime in reindeer]
    elvesChannels = [ch for name, ch, waitTime in elves]
    reindeerCases = [ch.receiveCase() for ch in reindeerChannels]
    elvesCases = [ch.receiveCase() for ch in elvesChannels]
    reindeerQueue = []
    elvesQueue = []

    cases = reindeerCases + elvesCases

    while True:
        ch, operation, value = stackless.select(cases)
        print value
        if ch in reindeerChannels: 
           reindeerQueue.append(value)
           if len(reindeerQueue) == 9:
              deliveryToys(reindeerQueue)
              reindeerQueue = []
        elif ch in elvesChannels:
           elvesQueue.append(value)
           if len(elvesQueue) == 3:
              consultWithSanta(elvesQueue)
              elvesQueue = []
        else:
           print "WHAT?"

    stopTwisted()
    print "Twisted is dead"


def makeWorkers(workers):
    for name, ch, waitTime in workers:
        stackless.tasklet(worker)(ch, name, waitTime)
    return 


if __name__ == "__main__":
   random.seed()
   reindeers = [(reindeer, stackless.channel(), RTIME) for reindeer in \
               ["DANCER", "PRANCER", "VIXEN", "COMET", "CUPID", "DONER", \
                "DASHER", "BLITZEN", "RUDOLPH"]]

   elves = [(elf, stackless.channel(), ETIME) for elf in \
            ['A','B','C','D','E','F','G','H','I','J']]

    
   makeWorkers(reindeers)
   makeWorkers(elves)

   l = task.LoopingCall(stackless.schedule)
   l.start(.001)
   stackless.tasklet(startTwisted)()
   stackless.tasklet(santa)(reindeers, elves)
   stackless.run()
_______________________________________________
Twisted-Python mailing list
[email protected]
http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python

Reply via email to