global interpreter lock
Michael Sparks
ms at cerenity.org
Thu Sep 15 15:50:03 EDT 2005
Michele Simionato wrote:
> It looks like I am reinventing Twisted and/or Kamaelia.
If it's /fun/ , is that a problem ? ;) (Interesting implementation BTW :)
FWIW, I've about a year ago it wasn't clear if we would be able to release
our stuff, so as part of a presentation I included a minimalistic decorator
based version of our system that has some similarities to yours. (The idea
was then at least the ideas had been shared, if not the main code - which
had approval)
Posted below in case it's of interest:
import copy
def wrapgenerator(bases=object, **attrs):
def decorate(func):
class statefulgenerator(bases):
__doc__ = func.__doc__
def __init__(self,*args):
super(statefulgenerator, self) __init__(*args)
self.func=func(self,*args)
for k in attrs.keys():
self.__dict__[k] = copy.deepcopy(attrs[k])
self.next=self.__iter__().next
def __iter__(self): return iter(self.func)
return statefulgenerator
return decorate
class com(object):
def __init__(_, *args):
# Default queues
_.queues = {"inbox":[],"control":[],
"outbox":[], "signal":[]}
def send(_,box,obj): _.queues[box].append(obj)
def dataReady(_,box): return len(_.queues[box])>0
def recv(_, box): # NB. Exceptions aren't caught
X=_.queues[box][0]
del _.queues[box][0]
return X
A sample component written using this approach then looks like this:
@wrapgenerator(com)
def forwarder(self):
"Simple data forwarding generator"
while 1:
if self.dataReady("inbox"):
self.send("outbox",self.recv("inbox"))
elif self.dataReady("control"):
if self.recv("control") == "shutdown":
break
yield 1
self.send("signal","shutdown")
yield 0
Since we're not actualy using this approach, there's likely to be border
issues here. I'm not actually sure I like this particular approach, but it
was an interesting experiment.
Best Regards,
Michael.
More information about the Python-list
mailing list