Hi Jay:
I modified the code you provided slightly:
import time
from pysage import Actor, ActorManager, Message
mgr = ActorManager.get_singleton()
class MainGroupMessage(Message):
properties = ['TenSecCount','ThreeSecCount']
types = ['i','i']
packet_type = 101
class ThreeSecGroupMessage(Message):
properties = []
packet_type = 102
class TenSecGroupMessage(Message):
properties = []
packet_type = 103
class MainAction(Actor):
subscriptions = ['MainGroupMessage']
def __init__(self):
Actor.__init__(self)
self.TenSecCount = 0
self.ThreeSecCount = 0
def handle_MainGroupMessage(self, msg):
if msg.get_property('TenSecCount') != 0 and
msg.get_property('TenSecCount')!= self.TenSecCount:
self.TenSecCount=msg.get_property('TenSecCount')
if msg.get_property('ThreeSecCount') != 0 and
msg.get_property('ThreeSecCount')!= self.ThreeSecCount:
self.ThreeSecCount=msg.get_property('ThreeSecCount')
print 'Ten Second Count %s -- Three Second Count %s' %
(self.TenSecCount,self.ThreeSecCount)
class ThreeSecAction(Actor):
Count=0
startTime = time.time()
subscriptions = ['ThreeSecGroupMessage']
def handle_ThreeSecGroupMessage(self, msg):
self.Count=self.Count+1
ActorManager.get_singleton().queue_message_to_group(ActorManager.get_singleton().PYSAGE_MAIN_GROUP,
MainGroupMessage(ThreeSecCount=self.Count, TenSecCount=0))
def update(self):
if time.time() - self.startTime >= 3.0:
ActorManager.get_singleton().queue_message(ThreeSecGroupMessage())
self.startTime=time.time()
class TenSecAction(Actor):
Count=0
startTime = time.time()
subscriptions = ['TenSecGroupMessage']
def handle_TenSecGroupMessage(self, msg):
self.Count=self.Count+1
ActorManager.get_singleton().queue_message_to_group(ActorManager.get_singleton().PYSAGE_MAIN_GROUP,
MainGroupMessage(TenSecCount=self.Count, ThreeSecCount=0))
def update(self):
if time.time() - self.startTime >= 10.0:
ActorManager.get_singleton().queue_message(TenSecGroupMessage())
self.startTime=time.time()
mgr.register_actor(MainAction())
mgr.add_process_group('ThreeSecGroup',ThreeSecAction)
mgr.add_process_group('TenSecGroup',TenSecAction)
while True:
processed = mgr.tick()
time.sleep(.03)
But a couple of things here:
1. try to use:"ActorManager.get_singleton()" as much as possible
instead of using the global "mgr". Because in forking systems, global
vars like "mgr" tend to "carry over" and will cause problems.
2. any type of Message class that's meant to be sent across processes
or network need to have "types" attribute specified in the Message
class. Pysage internally packages message into a C struct and send
across wire. It needs this information to do the (un)packing.
Thanks for the great example. Needless to say, pysage grouping
probably can provide better exception handling and documentation than
what it offers now. I'm looking to add that in the coming couple of
days. If you don't mind, can I use a variation of your sample code
for illustration?
Best,
John
On Tue, Apr 27, 2010 at 9:23 PM, Jay Herrmann <
jay.he...@gmail.com> wrote:
> Hi,
>
> I recently found the pysage project. I am excited to use it on
> project. I have reviewed the wiki and docs. But I am unable to get
> the groups example code to work in my sample app. Could someone put
> together a simple example (with groups) like:
https://code.google.com/p/pysage/wiki/SimpleUsage
>
> An Example I thought would demonstrative this would be python code
> something like this: (Note: I am new to python, but have used other
> languages like C, Php, C++, Object Pascal. I tried a was not able to
> get the following to work.)
>
> import time
> from pysage import Actor, ActorManager, Message
>
> mgr = ActorManager.get_singleton()
>
> class MainGroupMessage(Message):
> TenSecCount=0
> ThreeSecCount=0
> properties = ['TenSecCount','ThreeSecCount']
> packet_type = 101
>
> class ThreeSecGroupMessage(Message):
> properties = ['']
> packet_type = 102
>
> class TenSecGroupMessage(Message):
> properties = ['']
> packet_type = 103
>
> class MainAction(Actor):
> subscriptions = ['MainGroupMessage']
> def handle_MainGroupMessage(self, msg):
> if msg.get_property('TenSecCount')!= self.TenSecCount:
> self.TenSecCount=msg.get_property('TenSecCount')
> if msg.get_property('ThreeSecCount')!= self.ThreeSecCount:
> self.ThreeSecCount=msg.get_property('ThreeSecCount')
> print 'Ten Second Count %s -- Three Second Count %s' %
> [self.TenSecCount,self.ThreeSecCount]
>
> class ThreeSecAction(Actor):
> Count=0
> startTime = time.time()
> subscriptions = ['ThreeSecGroupGroupMessage']
> def handle_ThreeSecGroupMessage(self, msg):
> self.Count=self.Count+1
> mgr.queue_message_to_group(mgr.PYSAGE_MAIN_GROUP,
> MainGroupMessage(ThreeSecCount=self.Count))
> def update(self):
> if time.time() - self.startTime >= 3.0:
> mgr.queue_message(ThreeSecGroupMessage())
> self.startTime=time.time()
>
> class TenSecAction(Actor):
> Count=0
> startTime = time.time()
> subscriptions = ['TenSecGroupMessage']
> def handle_TenSecGroupMessage(self, msg):
> self.Count=self.Count+1
> mgr.queue_message_to_group(mgr.PYSAGE_MAIN_GROUP,
> MainGroupMessage(TenSecCount=self.Count))
> def update(self):
> if time.time() - self.startTime >= 10.0:
> mgr.queue_message(TenSecGroupMessage())
> self.startTime=time.time()
>
> mgr.register_actor(MainAction())
> mgr.add_process_group('ThreeSecGroup',ThreeSecAction)
> mgr.add_process_group('TenSecGroup',TenSecAction)
>
> while True:
> processed = mgr.tick()
> time.sleep(.03)
>
> As I said before, I am still learning Python. So I may be my
> incorrect programming. If so, is my use of the groups correct?
>
>
> Thanks,
> Jay
>
> --
> You received this message because you are subscribed to the Google Groups "pysage" group.
> To post to this group, send email to
pys...@googlegroups.com.
> To unsubscribe from this group, send email to
pysage+un...@googlegroups.com.
> For more options, visit this group at
http://groups.google.com/group/pysage?hl=en.
>
>
--
You received this message because you are subscribed to the Google Groups "pysage" group.
To post to this group, send email to
pys...@googlegroups.com.
To unsubscribe from this group, send email to
pysage+un...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/pysage?hl=en.