Thread's, async_chat and asyncore

Matt Hammond matt.hammond at rd.bbc.co.uk
Tue Oct 4 07:06:22 EDT 2005


Hi Jos,

Have you looked at Kamaelia? Its a project I'm involved in to create a  
framework for highly concurrent systems, geared particularly for  
suitability for network server/client applications.

A system is built out of many small components, each with their own  
pseudo-thread of execution (implemented as python generators). They  
communicate with each other by reading from and sending to local inboxes  
and outboxes. These are then linked together, causing messages to be  
passed from component to component.

We're hopefully going to be making a new release soon, with some more  
interesting facilities. A Kamaelia system doing something similar could  
look something like this:

----code snippet starts----

#!/usr/bin/env python

 from Axon.Component import component
import time
import random

class PulseGenerator(component):
     def __init__(self, msg):
         super(PulseGenerator,self).__init__()
         self.msg = msg

     def main(self):                     # microprocess (generator)
         t=time.time()
         while 1:
             while time.time() < t:
                 yield 1                 # hand back control to scheduler
             t += random.random()
             self.send( self.msg, "outbox")


if __name__ == "__main__":
     from Kamaelia.Util.Splitter import Plug, PlugSplitter
     from Kamaelia.Chassis.ConnectedServer import SimpleServer
     from Kamaelia.Util.passThrough import passThrough
     from Axon.Scheduler import scheduler

     producer = PlugSplitter(PulseGenerator("hello!\n")).activate()

     def newClientHandler():
         return Plug(producer, passThrough()).activate()

     SimpleServer( protocol = newClientHandler, port = 1601 ).activate()

     scheduler.run.runThreads()

----code snippet ends----

The SimpleServer component uses the factory function 'newClientHandler' to  
create a component to handle the new client connection. In this case, its  
a 'plug' component that connects to a splitter component. The purpose of  
the splitter is to copy output to multiple destinations. In this case, the  
output comes from a PulseGenerator component.

In the above system there's only one producer. To deal with multiple  
games, I guess you'd have to write a component that handles the initial  
'conversation' with the client to establish which game they want to  
connect to, before then creating the 'plug' component to connect to the  
appropriate game server.

This is of course only one way to do it, and the 'plug' arrangement might  
not be the best eventual solution. We've also got a lookup-service, called  
the Co-ordinating Assistant Tracker. It translates name to references to  
inboxes or outboxes on components. You could, for example, use this  
service to look up components handling specific games.

I'd encourage you to take a look at our project site on sourceforge  
( http://kamaelia.sf.net/ ). There's some more introductory material and a  
tutorial or two which should give you a better idea about the system.

We'd be interested to know what you think, and whether you think you could  
build your application using it.

regards


Matt


-- 

| Matt Hammond
| R&D Engineer, BBC Research & Development, Tadworth, Surrey, UK.
| http://kamaelia.sf.net/
| http://www.bbc.co.uk/rd/



More information about the Python-list mailing list