I'm running a python script as a service that takes some input and gives output. (I'm running it as a service because it takes a lot of time to initialize, so rather than running the script I'm keeping it up all the time as a service)
I used to call it from Flask server this way:
output, err = subprocess.Popen(["python3", SCRIPT_FILE_PATH, "--question", question], stdout=subprocess.PIPE).communicate()
Python Script:
from haystack.reader import FARMReader
reader = FARMReader(model_name_or_path="###", use_gpu=False)
if __name__ == "__main__":
# I did this when running this as a script to get the input
parser = argparse.ArgumentParser()
parser.add_argument('--question', type=str, required=True)
args = parser.parse_args()
question = args.question
# code stuff
print(output)
Service file:
[Unit]
Description=Test Service
After=multi-user.target
Conflicts=getty@tty1.service
[Service]
Type=simple
ExecStart=/usr/bin/python /home/root/test_service.py
StandardInput=tty-force
[Install]
WantedBy=multi-user.target
How do I communicate with this service?
I created a JSON file in /tmp and storing data in it that needs to move between the server and the service. Its a bit crude, but works for now.