Writing a Service running on local machine


In [1]:
from ciworker.base_worker import BaseWorker
from ciworker import arg_parser as parser
import networkx as nx
from networkx.readwrite import json_graph

class SimpleWorker(BaseWorker):

    def __init__(self, endpoint, id, router, collector, receiver):

        description = 'Generate a graph using network X'
        parameters = {
            "n": {
                "type": "number",
                "description": "Number of nodes in the new graph.",
                "required": True
            }
        }

        super(SimpleWorker, self).__init__(endpoint, description,
                                          parameters, id, router,
                                          collector, receiver)

    def run(self, data):
        
        if 'n' not in data.keys():
            # Use default
            n = 100
        else:
            n = data['n']
        
        graph = nx.scale_free_graph(n)

        return json_graph.node_link_data(graph)


---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-1-4e8d0a752bdf> in <module>()
----> 1 from ciworker.base_worker import BaseWorker
      2 from ciworker import arg_parser as parser
      3 import networkx as nx
      4 from networkx.readwrite import json_graph
      5 

ImportError: No module named 'ciworker'

In [5]:
# Register the service
graph_generator = SimpleWorker(
        endpoint='scalefree',
        id=10,
        router='service',
        collector='collector',
        receiver=5599
    )

graph_generator.listen()


---------------------------------------------------------------------------
ConnectionError                           Traceback (most recent call last)
<ipython-input-5-6b5695c23094> in <module>()
      5         router='service',
      6         collector='collector',
----> 7         receiver=5599
      8     )
      9 

<ipython-input-4-4e8d0a752bdf> in __init__(self, endpoint, id, router, collector, receiver)
     19         super(SimpleWorker, self).__init__(endpoint, description,
     20                                           parameters, id, router,
---> 21                                           collector, receiver)
     22 
     23     def run(self, data):

/usr/local/lib/python2.7/site-packages/ciworker/base_worker.pyc in __init__(self, endpoint, description, parameters, id, router, collector, receiver, sender, monitor, result_server, redis_server)
     41         context = zmq.Context()
     42 
---> 43         registered = self.redis_conn.hgetall('endpoints')
     44         if endpoint not in registered.keys():
     45             self.redis_conn.hset('endpoints', endpoint, receiver)

/usr/local/lib/python2.7/site-packages/redis/client.pyc in hgetall(self, name)
   1826     def hgetall(self, name):
   1827         "Return a Python dict of the hash's name/value pairs"
-> 1828         return self.execute_command('HGETALL', name)
   1829 
   1830     def hincrby(self, name, key, amount=1):

/usr/local/lib/python2.7/site-packages/redis/client.pyc in execute_command(self, *args, **options)
    568             if not connection.retry_on_timeout and isinstance(e, TimeoutError):
    569                 raise
--> 570             connection.send_command(*args)
    571             return self.parse_response(connection, command_name, **options)
    572         finally:

/usr/local/lib/python2.7/site-packages/redis/connection.pyc in send_command(self, *args)
    554     def send_command(self, *args):
    555         "Pack and send a command to the Redis server"
--> 556         self.send_packed_command(self.pack_command(*args))
    557 
    558     def can_read(self):

/usr/local/lib/python2.7/site-packages/redis/connection.pyc in send_packed_command(self, command)
    530         "Send an already packed command to the Redis server"
    531         if not self._sock:
--> 532             self.connect()
    533         try:
    534             if isinstance(command, str):

/usr/local/lib/python2.7/site-packages/redis/connection.pyc in connect(self)
    434         except socket.error:
    435             e = sys.exc_info()[1]
--> 436             raise ConnectionError(self._error_message(e))
    437 
    438         self._sock = sock

ConnectionError: Error 8 connecting to redis:6379. nodename nor servname provided, or not known.