Мои критерии:
- запустить Win и java-приложение
- выполнить команду в jav-приложении
- дождаться ответа от jav-приложения
- использовать ответ от java-приложение в приложение Windows в качестве входных данных.
- выполнить команду в Win-приложении
- дождаться ответа от Win-приложения
< li>использовать ответ из приложения Win в приложение Java в качестве входных данных и повторить.
Мне трудно реализовать мьютекс или двоичный семафор.
Вот фрагмент кода, который я пытаюсь завершить:
Код: Выделить всё
import datetime
import time
import multiprocessing
class Win_App():
def __init__(self):
self.input_command = [] # list of string
self.output_log = [] #list of string
self.next_jav_command=''
def execute_win_application(self):
exe_path = 'path_ to_exe'
init_command= 'command to execute '
win_process = subprocess.Popen('cmd.exe', stdin=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, text=True)
win_process.stdin.write(exe_path)
win_process.stdin.write(init_command)
print('Win Application Started !')
while True:
#TODO: mutex/semaphore acquire and start tasks
win_process.stdin.write(self.input_command[-1]) # execute the last command in the list
for lines in process.stdout():
write_into_file("win_app_output.log",line)
self.output_log.append(lines)
self.next_jav_command = parse_lines_and_get_command()
if (nex_win_command == '-1'):
self.next_jav_command == 'exit'
break:
# TODO: hold the process here and move to Java application
# TODO: release mutex/ semaphore
class Java_App():
def __init__(self):
self.input_command = [] # list of string
self.output_log = [] #list of string
self.win_next_command = ''
def execute_java_application(self):
jav_path = 'path_ to_java_app'
init_command= 'command to execute '
jav_process = subprocess.Popen('cmd.exe', stdin=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, text=True)
print('Java Application Started!')
while True:
#TODO: mutex/semaphore acquire and start tasks
jav_process.stdin.write(self.input_command[-1]) # execute the last command in the list
for lines in process.stdout():
write_into_file("jav_app_output.log",line)
self.output_log.append(lines)
nex_win_command = parse_lines_and_get_command()
if (nex_win_command == 'exit'):
break:
# TODO: hold the process here and move to Java application
# TODO: release mutex/ semaphore
jav_process.kill()
##### MAIN PROGRAM #####
if __name__ == '__main__':
# Initialize process given certain parameters
win = Win_App()
jav = Java_App()
p1 = multiprocessing.Process(target=win.execute_win_application)
p1.start()
print(" win App started!")
p2 = multiprocessing.Process(target=jav.execute_java_application)
p2.start()
while True:
# TODO : implement context switch between process in execution
# like a binary semaphore or mutex
Подробнее здесь: https://stackoverflow.com/questions/787 ... -in-python