def setGet(newSettings=None): # Returns dictionary global ipcSocketPath if newSettings == None: kwargs = {'query': ''} # This can really be anything other than 'json' else: kwargs = {'json': json.dumps(newSettings)} user_input = [{ 'class': 'Event', 'args': ['newSettings'], 'kwargs': kwargs }] objects = ipc.Message.deserialize(user_input) try: with ipc.Client(ipcSocketPath) as client: response = client.send(objects) except Exception as e: logToFile("failed to send: " + str(e)) pass try: responseDict = json.loads(response[0].text) except Exception as e: logToFile("failed to load json: " + str(e)) responseDict = None pass return responseDict
def predict(image_path): """Send image_path to ipc_server which is running forever for providing annotation results.""" server_address = ('localhost', 5795) with ipc.Client(server_address) as client: result = client.send(image_path) # avoid duplicate labels in rare cases result = list(set(result)) return result
def __init__(self, fname=None): if not fname: fname = find_sockfile() self.client = ipc.Client(fname) _CommandRoot.__init__(self)
async def run(opts): mapping = config.json.load(opts["config"]) preferences = config.from_mapping(mapping) logger = log.new_logger("avatar", log.Verbosity.DEBUG, log.SIMPLE_TEXT_FORMAT) logger.info("Starting avatar process with interval %d.", preferences.avatar_interval) container = di.default_container container.register(config.Config, preferences) container.register(logging.Logger, logger) container.register(avatar.Connection, sqlite.Connection(preferences.database_filename)) container.register(avatar.Reader, avatar.sqlite.Reader()) container.register( avatar.Writer, avatar.sqlite.Writer(preferences.avatar_reload_timeout, preferences.avatar_retry_timeout, preferences.avatar_max_errors, preferences.avatar_error_timeout)) container.register( avatar.Storage, avatar.fs.AsciiFiles(preferences.avatar_directory, preferences.avatar_ascii_width, preferences.avatar_ascii_height)) client = ipc.Client(preferences.server_ipc_binding) download = Download() conn_f = await client.connect() msg_f = asyncio.ensure_future(client.read()) timeout_f = asyncio.ensure_future(asyncio.sleep(1)) clean_f = asyncio.ensure_future( asyncio.sleep(preferences.avatar_cleanup_interval)) if os.name == "posix": loop = asyncio.get_event_loop() logger.debug("Registerung SIGINT handler.") loop.add_signal_handler(signal.SIGINT, lambda: None) quit = False class Action(Enum): NONE = 0 FETCH = 1 CLEANUP = 2 QUIT = 3 while not quit: done, _ = await asyncio.wait([conn_f, msg_f, timeout_f, clean_f], return_when=asyncio.FIRST_COMPLETED) action = Action.NONE for f in done: if f is msg_f: receiver, message = msg_f.result() if receiver == "avatar": logger.debug("Message received: '%s'", message) if message == "put": action = Action.FETCH elif f is conn_f: action = Action.QUIT elif f is timeout_f: action = Action.FETCH elif f is clean_f: action = Action.CLEANUP if action == Action.QUIT: quit = True elif action == Action.FETCH: download.fetch() elif action == Action.CLEANUP: download.cleanup() for f in done: if f is msg_f: msg_f = asyncio.ensure_future(client.read()) elif f is timeout_f: timeout_f = asyncio.ensure_future( asyncio.sleep(preferences.avatar_interval)) elif f is clean_f: clean_f = asyncio.ensure_future( asyncio.sleep(preferences.avatar_cleanup_interval)) logger.info("Stopped.")
async def run(opts): mapping = config.json.load(opts["config"]) preferences = config.from_mapping(mapping) logger = log.new_logger("mail", log.Verbosity.DEBUG, log.SIMPLE_TEXT_FORMAT) logger.info("Starting mail process with interval %.2f.", preferences.mail_interval) container = di.default_container container.register(config.Config, preferences) container.register(logging.Logger, logger) container.register(mail.Connection, sqlite.Connection(preferences.database_filename)) container.register(mail.Queue, mail.sqlite.Queue(preferences.mail_ttl, preferences.mail_max_errors, preferences.mail_retry_timeout)) container.register(mail.MTA, mail.smtp.MTA(preferences.smtp_hostname, preferences.smtp_port, preferences.smtp_ssl_enabled, preferences.smtp_start_tls, preferences.smtp_sender, preferences.smtp_username, preferences.smtp_password)) client = ipc.Client(preferences.server_ipc_binding) mailer = Sendmail() conn_f = await client.connect() msg_f = asyncio.ensure_future(client.read()) cleanup_f = asyncio.ensure_future(asyncio.sleep(0)) timeout_f = asyncio.ensure_future(asyncio.sleep(1)) if os.name == "posix": loop = asyncio.get_event_loop() logger.debug("Registerung SIGINT handler.") loop.add_signal_handler(signal.SIGINT, lambda: None) quit = False class Action(Enum): NONE = 0 SEND = 1 CLEANUP = 2 QUIT = 3 while not quit: done, _ = await asyncio.wait([conn_f, msg_f, cleanup_f, timeout_f], return_when=asyncio.FIRST_COMPLETED) action = Action.NONE for f in done: if f is msg_f: receiver, message = msg_f.result() if receiver == "mail": logger.debug("Message received: '%s'", message) if message == "put": action = Action.SEND elif f is cleanup_f: action = Action.CLEANUP elif f is timeout_f: action = Action.SEND elif f is conn_f: action = Action.QUIT if action == Action.SEND: mailer.send() elif action == Action.CLEANUP: mailer.cleanup() elif action == Action.QUIT: quit = True for f in done: if f is msg_f: msg_f = asyncio.ensure_future(client.read()) if f is cleanup_f: cleanup_f = asyncio.ensure_future(asyncio.sleep(preferences.mail_cleanup_interval)) if f is timeout_f: timeout_f = asyncio.ensure_future(asyncio.sleep(preferences.mail_interval)) logger.info("Stopped.")
import ipc ipc.Client("127.0.0.1") ipc.Client("127.0.0.1") ipc.Client("127.0.0.1") ipc.Client("127.0.0.1") ipc.Client("127.0.0.1") ipc.Client("127.0.0.1") ipc.Client("127.0.0.1") ipc.Client("127.0.0.1")
response = [Response('Recieved {} objects'.format(len(objects)))] print 'Recieved objects: {}'.format(objects) print 'Sent objects: {}'.format(response) return response if __name__ == '__main__': args = docopt.docopt(__doc__) server_address = args['--socket'] or (args['--host'], int(args['--port'])) if args['server']: ipc.Server(server_address, server_process_request).serve_forever() if args['client']: kwargs = {k: v for k, v in [i.split('=', 1) for i in args['--kwarg']]} user_input = [{ 'class': args['<class>'], 'args': args['--arg'], 'kwargs': kwargs }] objects = ipc.Message.deserialize(user_input) print 'Sending objects: {}'.format(objects) with ipc.Client(server_address) as client: response = client.send(objects) print 'Recieved objects: {}'.format(response) # Example usage: # $ ./ipc_example.py server # then in another terminal: # $ ./ipc_example.py client Event --arg=testevent --kwarg=exampleproperty=examplevalue