I'm trying to automate a sync relation between a win application and a java application. My criteria is :
- start win and jav application
- execute command in jav application
- wait for response from jav application
- use the response from jav application into windows application as input.
- execute command in win application
- wait for response from win application
- use the response from win application into java application as input and repeat.
(Note we're executing commands within the console of the application i.e. both applications are required to be active processes.)
I'm having a hard time trying to implement a mutex or a binary semaphore.
Here is a code snippet I'm trying to complete:
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
I'm able to handle the miniature functions/methods but the I'm unable to implement the #TODO tasks in above code.