def main(): logging.basicConfig(level=logging.DEBUG) address = sys.argv[1] service = Service(address) service.register('test', test) service.start()
def start(configpath): """ Read config file and start service """ global CONF CONF = util.prepare(configpath) service = Service(CONF.address, encoder=JSONEncoder()) service.register('send', send) util.enhance(service) service.start()
class Program(BaseProgram): """ Long running program with a nanoservice endpoint. `service` - nanoservice Service object `config` - the configuration parsed from --config <filepath> """ def __init__(self, description, address): super(Program, self).__init__(description, address) self.service = Service(address) if address else None self.config = compat.configparser.ConfigParser() # Add the flag for parsing configuration file self.parser.add_argument( '--config', help='configuration file to use', nargs='?') if self.service is None: return # Add default service worker, which will respond to ctl commands # Other workers will perform other kind of work, such as # fetching resources from the web, etc self.workers.append(worker.ServiceWorker(self.service)) # Add default commands self.add_command('ping', lambda: 'pong') self.add_command('help', self.help_function) def help_function(self, command=None): """ Show help for all available commands or just a single one """ if command: return self.registered[command].get( 'description', 'No help available' ) return ', '.join(sorted(self.registered)) def add_command(self, command, function, description=None): """ Register a new function for command """ super(Program, self).add_command(command, function, description) self.service.register(command, function) def run(self, args=None): """ Parse comand line arguments/flags and run program """ args = args or self.parser.parse_args() super(Program, self).run(args) # Read configuration file if any if args.config is not None: filepath = args.config self.config.read(filepath) # Start workers then wait until they finish work [w.start() for w in self.workers] [w.join() for w in self.workers]
class Program(BaseProgram): """ Long running program with a nanoservice endpoint. `service` - nanoservice Service object `config` - the configuration parsed from --config <filepath> """ def __init__(self, description, address): super(Program, self).__init__(description, address) self.service = Service(address) if address else None self.config = compat.configparser.ConfigParser() # Add the flag for parsing configuration file self.parser.add_argument('--config', help='configuration file to use', nargs='?') if self.service is None: return # Add default service worker, which will respond to ctl commands # Other workers will perform other kind of work, such as # fetching resources from the web, etc self.workers.append(worker.ServiceWorker(self.service)) # Add default commands self.add_command('ping', lambda: 'pong') self.add_command('help', self.help_function) def help_function(self, command=None): """ Show help for all available commands or just a single one """ if command: return self.registered[command].get('description', 'No help available') return ', '.join(sorted(self.registered)) def add_command(self, command, function, description=None): """ Register a new function for command """ super(Program, self).add_command(command, function, description) self.service.register(command, function) def run(self, args=None): """ Parse comand line arguments/flags and run program """ args = args or self.parser.parse_args() super(Program, self).run(args) # Read configuration file if any if args.config is not None: filepath = args.config self.config.read(filepath) # Start workers then wait until they finish work [w.start() for w in self.workers] [w.join() for w in self.workers]
class BaseTestCase(unittest.TestCase): def setUp(self): addr = 'inproc://test' self.client = Client(addr) self.service = Service(addr) self.service.register('divide', lambda x, y: x / y) self.service.register('echo', lambda x: x) def tearDown(self): self.client.sock.close() self.service.sock.close()
class BaseTestCase(unittest.TestCase): def setUp(self): addr = 'inproc://test' self.client = Client(addr) self.service = Service(addr) self.service.register('divide', lambda x,y: x/y) self.service.register('echo', lambda x: x) def tearDown(self): self.client.sock.close() self.service.sock.close()
def start_service(addr, n): """ Start a service """ s = Service(addr) s.register('add', lambda x, y: x + y) started = time.time() for _ in range(n): s.process() duration = time.time() - started time.sleep(0.1) print('Service stats:') util.print_stats(n, duration) return
def start(configpath): """ Read config file and start service """ global CONF CONF = util.prepare(configpath) service = Service(CONF.address, encoder=JSONEncoder()) util.enhance(service) service.register('decode_jwt', decode_jwt) service.register('generate_jwt', generate_jwt) service.register('generate_sms', generate_sms) service.register('validate_sms', validate_sms) service.start()
def __init__(self, description, address): super(Program, self).__init__(description, address) self.service = Service(address) if address else None self.config = compat.configparser.ConfigParser() # Add the flag for parsing configuration file self.parser.add_argument('--config', help='configuration file to use', nargs='?') if self.service is None: return # Add default service worker, which will respond to ctl commands # Other workers will perform other kind of work, such as # fetching resources from the web, etc self.workers.append(worker.ServiceWorker(self.service)) # Add default commands self.add_command('ping', lambda: 'pong') self.add_command('help', self.help_function)
def start_service(addr, n): """ Start a service """ s = Service(addr) started = time.time() for _ in range(n): msg = s.sock.recv() s.sock.send(msg) s.sock.close() duration = time.time() - started print('Raw REP service stats:\n') util.print_stats(n, duration) return
def __init__(self, description, address): super(Program, self).__init__(description, address) self.service = Service(address) if address else None self.config = compat.configparser.ConfigParser() # Add the flag for parsing configuration file self.parser.add_argument( '--config', help='configuration file to use', nargs='?') if self.service is None: return # Add default service worker, which will respond to ctl commands # Other workers will perform other kind of work, such as # fetching resources from the web, etc self.workers.append(worker.ServiceWorker(self.service)) # Add default commands self.add_command('ping', lambda: 'pong') self.add_command('help', self.help_function)
def start_service(self, addr): s = Service(addr) s.register('divide', lambda x, y: x / y) s.start()
import logging from nanoservice import Service def greet(name): return 'Hello {}'.format(name) def add(x, y): return x + y s = Service('ipc:///tmp/service.sock') s.register('greet', greet) s.register('add', add) s.start()
def setUp(self): addr = 'inproc://test' self.client = Client(addr) self.service = Service(addr) self.service.register('divide', lambda x,y: x/y) self.service.register('echo', lambda x: x)
def start_service(self, addr): s = Service(addr) s.register('divide', lambda x,y: x/y) s.start()
def setUp(self): addr = 'inproc://test' self.client = Client(addr) self.service = Service(addr) self.service.register('divide', lambda x, y: x / y) self.service.register('echo', lambda x: x)