Glad to hear your project is moving forward. You are on the right
track. Besides the "C types", there is also an array and an arbitrary
string type built into pysage.
Please see:
http://www.bigjstudio.com/pysage/network.html#network-messages
You should be able to simulate a dictionary with an array. The
provided automatic packing/unpacking can support arbitrary types.
However, you do need to provide the functions that actually do the
packing/unpacking. What is the type of the key and value for the
dictionary? I'll be happy to provide you with an example if you give
me some more details.
HTH,
John
> --
> 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.
>
>
Where MainGroup would have properties like
(Action='DoSomething',Dict={'Data': 'to work with'})
So SubGroupX could pass data to MainGroup for some action
As I did a little more research, I did find at Python 2.6>= supports
JSON. So I could do something like below that used a type of 'S'.
>>> import json
>>> json.dumps(c)
'{"date": {"BTID": {"action": [1, 2], "date": "now"}}}'
>>> js = json.dumps(c)
>>> js
'{"date": {"BTID": {"action": [1, 2], "date": "now"}}}'
>>> d = json.loads(js)
>>> d
{u'date': {u'BTID': {u'action': [1, 2], u'date': u'now'}}}
The SubGroupX would encode dict as JSON string pass to MainGroup then
Decode JSON string back to dict. Speed of encoding and decoding maybe
an issue in the future as I am looking at putting this on a embedding
ARM brd. But for testing and design on an x86 I think this should
work. I am hoping. :)
Thanks,
Jay
First of all, update pysage :). Small patch to enable atomic type
packing/unpacking (only had packing/unpacking for composite types
before).
Here I have a small example on how to use the (un)packing with JSON.
from pysage import *
import json
import time, random
class FoodAvailableMessage(Message):
properties = ['food']
types = ['S']
packet_type = 101
def pack_food(self, food):
return json.dumps(food)
def unpack_food(self, food_s):
return json.loads(food_s)
class Consumer(Actor):
subscriptions = ['FoodAvailableMessage']
def handle_FoodAvailableMessage(self, msg):
print 'Yummy! I had %d %s pancakes!' %
(msg.get_property('food')['amount'],
msg.get_property('food')['color'])
import time, random
class Chef(Actor):
def __init__(self):
self.last_sent = time.time()
def update(self):
'''every 2 seconds, this chef makes a random amount of pancakes'''
if time.time() - self.last_sent > 2.0:
mgr.queue_message_to_group(mgr.PYSAGE_MAIN_GROUP,
FoodAvailableMessage(food={'amount': random.randint(0,10), 'color':
'red'}))
self.last_sent = time.time()
mgr = ActorManager.get_singleton()
mgr.register_actor(Consumer())
if __name__ == '__main__':
mgr.enable_groups()
mgr.add_process_group('chefs', Chef) # spawns a new process
that "ticks" independently
while True:
processed = mgr.tick()
time.sleep(.03)
HTH. You will incur the cost of JSON serialization. However, you do
have small enough message now. In the future, you can take advantage
of the composite type and array type to easily mimic this behaviour
without using JSON. Let me know if I can provide an example for that
as well should you need it.
John