들어오는 명령을 기다리는 백그라운드 프로세스를 만들어야합니다.Python의 백그라운드 프로세스
instance_tuple.popen = subprocess.Popen(['python',\
os.path.join(config['scripts_dir'],\
'instance_script.py')],\
stdin = subprocess.PIPE,\
stdout = subprocess.PIPE)
프로세스 기능 코드 :
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'
내가 프로세스에 데이터를 전송하는 방법은 다음과 같습니다 여기 코드는 1 차 테스트 실행 괜찮 작동합니다
(input, errors) = instance_tuple.popen \
.communicate(json.dumps({'name': 'name', 'args': list()}))
나중에 어떤 이유로 raw_input()
에 대한 EOF를 얻고 프로세스가 종료됩니다. 프로세스 간 통신을 설정하는 올바른 방법은 무엇입니까?
덕분에, 나는 그로 살펴 보겠습니다. –
MQ [비교 기사] (http://wiki.secondlife.com/wiki/Message_Queue_Evaluation_Notes)에서 파이썬 zeromq 확장이 깨졌습니다. 이 문제가 해결 되었습니까? –
주목할 또 하나 :'pyzmq'는 json이나 pickled python 객체를 보내기위한 멋진 메소드를 가지고 있습니다. 또한 'client.send_json ({name': 'name', 'args': list()})'또는'client.send_pyobj()'를 호출한다. – zeekay