I need to create a background process that will wait for incoming commands and perfom them. Here's 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'
That's how I send data to the process: 1st test run works ok:
(input, errors) = instance_tuple.popen \
.communicate(json.dumps({'name': 'name', 'args': list()}))
Later for some reason raw_input() gets a EOF and the process exits. What is the correct way to setup an interprocess communication?