Re: Groups Code ExampleHi, 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 exa

4 views
Skip to first unread message
Message has been deleted

John Yang

unread,
Apr 28, 2010, 5:35:29 PM4/28/10
to pys...@googlegroups.com
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.

Jay Herrmann

unread,
Apr 28, 2010, 10:09:01 PM4/28/10
to pysage
Thanks for code changes. After I post it, I was wondering if python
had a constructor. I see it is __init__(self). I will tests the
changes tonight or tomorrow.


Yes, feel free to use the example code. I was hoping it would help
others understand the grouping.

The ActorManager.get_singleton() was something I thought when I was
reading your docs but forget about it when I was creating the code.

Thank you for the object based Python messaging code.

Jay Herrmann

unread,
Apr 28, 2010, 10:27:58 PM4/28/10
to pysage
I just updated the code for the Grouping example to the files section:
SampleGroupMessaging.py

It worked with pysage.py 1.5.2.

It very cool. :)

Jay.

John Yang

unread,
May 7, 2010, 1:43:57 PM5/7/10
to pys...@googlegroups.com
Jay:

I've uploaded pysage version 1.5.3. This fixes some of the group bugs
you discovered and combines other fixes since 1.5.2.

I also uploaded a newer version of the your sample file here:
http://groups.google.com/group/pysage/files

Couple of changes:
1. Calling "ActorManager.get_singleton()" is not required every time anymore.
2. I wrapped the grouping calls in a __name__ == "__main__". This is
python's way of a "main" function. This is important for non-forking
systems like Windows so that it does not spawn child processes
endlessly.
3. Now, you need to call manager.enable_groups() before using groups
feature, which prepares certain things for when your program gets
"frozen" into an executable. Mainly for windows.

That's about it. Do an "easy_install" and you should have the latest
version. I'll look to update the "grouping" docs soon.

John
Reply all
Reply to author
Forward
0 new messages