python - 如何为作为服务运行的 Python 脚本提供输入

标签 python python-3.x ubuntu service

我正在运行一个 python 脚本作为服务,它接受一些输入并提供输出。 (我将它作为服务运行,因为初始化需要很长时间,所以我没有运行脚本,而是一直将它作为服务运行)
习惯以这种方式从 Flask 服务器调用它:

output, err = subprocess.Popen(["python3", SCRIPT_FILE_PATH, "--question", question], stdout=subprocess.PIPE).communicate()
Python脚本:
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)
    
服务文件:
[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
我如何与此服务通信?

最佳答案

我在/tmp 中创建了一个 JSON 文件,并将需要在服务器和服务之间移动的数据存储在其中。它有点粗糙,但现在可以使用。

关于python - 如何为作为服务运行的 Python 脚本提供输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68608065/

相关文章:

python - 将嵌套字典写入 csv

python - 当实现两个玩家时 Pygame 会滞后

user-interface - Ubuntu 18.04 服务器安装程序使用什么 TUI?

mysql - 在 ubuntu 16.04 中安装和使用 Mysql-proxy 时出现问题

python - 设置 kivy 整数的最小值和最大值

ubuntu - 安装kaldi时libmkl_tbb_thread.so相关的一个问题

python - 我可以使用 SQLAlchemy 使用聚合表达式吗?

python - 属性错误: can't set attribute - Python instance attribute assignment

api - Cornice 中的cornice.Service 和cornice.resource 有什么区别?

python - Flask:如何自动化 OpenAPI v3 文档?