Distributed Executor Service in Python

126 views
Skip to first unread message

max.ro...@agentscompany.com

unread,
Dec 6, 2016, 12:00:22 AM12/6/16
to Hazelcast
Hi All,

Can anyone point me to an example or any documentation on how to use the distributed executor service from Python please?

The documentation has not indication as to how one should initialize "Task" object

See:  
api/hazelcast.proxy.executor.html

**********************************************
execute_on_members(members, task)

Executes a task on each of the specified members.

Parameters:
  • members – (Collection), the specified members.
  • task – (Task), the task executed on the specified members.
Returns:

(Map), Future tuples representing pending completion of the task on each member.

**********************************************
(The online version, http://hazelcast.github.io/hazelcast-python-client/, is broken BTW )






M. Sancar Koyunlu

unread,
Dec 13, 2016, 2:47:18 AM12/13/16
to Hazelcast
Hi, 
You need to implement Task object on java side. Then implement corresponding class in client side with same factory id and class id to match to server side. I have prepared an example code for you. 

import hazelcast
import logging
from hazelcast.serialization.api import IdentifiedDataSerializable

class CollectData(IdentifiedDataSerializable):
CLASS_ID = 12
FACTORY_ID = 3

def __init__(self, propertyKey):
self.propertyKey = propertyKey

def write_data(self, out):
out.write_utf(self.propertyKey)

def get_factory_id(self):
return self.FACTORY_ID

def get_class_id(self):
return self.CLASS_ID


if __name__ == '__main__':
logging.basicConfig(format='%(asctime)s%(msecs)03d [%(name)s] %(levelname)s: %(message)s', datefmt="%H:%M%:%S,")
logging.getLogger().setLevel(logging.INFO)
logger = logging.getLogger("main")

config = hazelcast.ClientConfig()
config.group_config.name = "dev"
config.group_config.password = "dev-pass"
config.network_config.addresses.append('127.0.0.1')

identifiedDataSerializable_factory = {CollectData.CLASS_ID: CollectData}
config.serialization_config.add_data_serializable_factory(CollectData.FACTORY_ID, identifiedDataSerializable_factory)

client = hazelcast.HazelcastClient(config)
executor = client.get_executor("my-exec")
results = executor.execute_on_all_members(CollectData("java.vm.name"))
for result in results.result():
print ">", result


Java side of this should look like as following:

import com.hazelcast.config.Config;
import com.hazelcast.config.SerializationConfig;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.nio.ObjectDataInput;
import com.hazelcast.nio.ObjectDataOutput;
import com.hazelcast.nio.serialization.DataSerializableFactory;
import com.hazelcast.nio.serialization.IdentifiedDataSerializable;

import java.io.IOException;
import java.util.concurrent.Callable;

class CollectData implements IdentifiedDataSerializable, Callable<String> {
public static int CLASS_ID = 12;
private String propertyKey;

CollectData() {

}

CollectData(String propertyKey) {
this.propertyKey = propertyKey;
}

public int getFactoryId() {
return 3;
}

public int getId() {
return 12;
}

public void writeData(ObjectDataOutput out) throws IOException {
out.writeUTF(propertyKey);
}

public void readData(ObjectDataInput in) throws IOException {
propertyKey = in.readUTF();
}

public String call() throws Exception {
return System.getProperty(propertyKey);
}
}

public class XServer {

public static void main(String[] args) {
Config config = new Config();
SerializationConfig serializationConfig = config.getSerializationConfig();
serializationConfig.addDataSerializableFactory(3, new DataSerializableFactory() {
public IdentifiedDataSerializable create(int typeId) {
if (CollectData.CLASS_ID == typeId) {
return new CollectData();
}
return null;
}
});
Hazelcast.newHazelcastInstance(config);

}
}



--
You received this message because you are subscribed to the Google Groups "Hazelcast" group.
To unsubscribe from this group and stop receiving emails from it, send an email to hazelcast+...@googlegroups.com.
To post to this group, send email to haze...@googlegroups.com.
Visit this group at https://groups.google.com/group/hazelcast.
To view this discussion on the web visit https://groups.google.com/d/msgid/hazelcast/f1bb8b51-eaaa-408e-93f0-88d7bf1eab18%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
Sancar Koyunlu
Software Engineer, Hazelcast

M. Sancar Koyunlu

unread,
Dec 13, 2016, 3:00:38 AM12/13/16
to Hazelcast
We are fixing this link http://hazelcast.github.io/hazelcast-python-client/  . Thanks for the report.

gramlerg...@googlemail.com

unread,
Dec 28, 2016, 6:09:24 AM12/28/16
to Hazelcast
I ... see

Thanks most kindly

This explains things nicely


Reply all
Reply to author
Forward
0 new messages