[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Networking?



On Wednesday 21 November 2007 06:51:38 Jason Ward wrote:
> Thanks for the options guys. I'll look into them when I find the time. But I
> don't mind low-level stuff if that's what sockets library requires, just as
> long as it can work.

Sure of course it'll work. The code is pretty much the same as you'd find for 
C based code. We're just trying to save you hassle :-)  If you enjoy it 
though, go for it :-)

> I looked into twisted but it's documentation is rather difficult and I hate 
> libraries that want the main loop, I prefer the control.

FWIW, Kamaelia differs from twisted in that, aside from network systems not
being its sole focus, in that it recognises that systems like games, network
servers and most interactive systems tend to need multiple threads of control
and focusses on making that simpler & clearer to work with as well as
composable. (networking was the first use case, but was never intended to
be the sole focus)

As a result _you_ create a component for each logical thread of control.
Performance wise this can be pretty similar to hardcoding it yourself (the
difference is around 5% last time I measured), but the benefit you get is
more explicit control over the main loop for each thing, with a built in way
of allowing communication between the main loops.

You can use threads to do this:

class ConsoleReader(Axon.ThreadedComponent.threadedcomponent):
   def main(self):
      while 1:
          X = raw_input(">>> ")
          self.send(X, "outbox")

class ConsoleEchoer(Axon.ThreadedComponent.threadedcomponent):
   def main(self):
      while 1:
          while self.dataReady("inbox"):
               X = self.recv("inbox")
               print X

And then join the dots between them:

Pipeline(
    ConsoleReader(),
    ConsoleEchoer(),
).run()

Or make the components run in the same thread as all the others by changing to 
a generator:

class ConsoleEchoer(Axon.Component.component):
   def main(self):
      while 1:
          while self.dataReady("inbox"):
               X = self.recv("inbox")
               print X
          yield 1

But either way, it allows you to have as many main loops *you* want in a piece 
of code that make sense in a relatively natural & scalable way. (Generator 
components are pretty much equivalent to state machines intermediate buffers, 
just somewhat more direct)

You don't have to use Kamaelia's networking or pygame components for it to
be useful, but if you're thinking "I need to do more than one thing at once,
maybe I should use threads", take a look. Kamaelia can't magically solve your 
problems of course, so this just a suggestion.

Unlike some systems, the core of Kamaelia isn't really the components
(which are IMO the useful bit), but really just a core concurrency framework -
Axon - which builds the support that enables the above. We've got a simple
tutorial that's targetted to explain the approach here where you build your
own core:
   http://kamaelia.sourceforge.net/MiniAxon/

And you can happily build systems using such a beast. Indeed, the first pygame 
components we had came from someone's mini-axon system.

I love pygame, but these days I'd never use it by itself :-)
(Even if Kamaelia isn't really quite the same model as pygame :-)

If you do go down the route of coding your own networking though, I'm sure 
others would find your ideas/approach interesting :-)

The networked pong example was a 20-30 minute hack BTW from start
to finish. I'm now MASSIVELY off topic though, so I'll stop there :-)


Michael.