I need to create a background process that will wait for incoming commands and execute them. Here is the code:
instance_tuple.popen = subprocess.Popen(['python',\ os.path.join(config['scripts_dir'],\ 'instance_script.py')],\ stdin = subprocess.PIPE,\ stdout = subprocess.PIPE)
Process Function Code:
if __name__ == '__main__': config = dict() is_config_valid = False print 'Hello from instance process' while True: cmd_str = raw_input() if (cmd_str.strip() != ''): print 'received %s' % cmd_str command = json.loads(cmd_str) print 'received command: %s' % str(command) sys.stdout.flush() if command['name'] == 'set_variable': name = command['args'][0] value = command['args'][1] config[name] = value is_config_valid = validate_instance_dict(config) elif is_config_valid: if (command['name'] == 'init_model'): config['instance'].init_model() elif (command['name'] == 'get_tree'): tree = config['instance'].get_fidesys_tree(command['args']) result = CommandResult(command.name, tree) print 'process exit'
The way I send data to the process: 1st test run works fine:
(input, errors) = instance_tuple.popen \ .communicate(json.dumps({'name': 'name', 'args': list()}))
Later, for some reason, raw_input() gets an EOF, and the process ends. What is the correct way to configure interprocess communication?
source share