【问题标题】:How do I provide input to Python script running as a service如何为作为服务运行的 Python 脚本提供输入
【发布时间】:2021-08-01 06:44:01
【问题描述】:

我正在运行一个 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

我如何与此服务通信?

【问题讨论】:

  • Windows 服务过去需要与桌面通信 权限才能执行此操作。但这现在被认为是一种安全风险,并且已经被有效地削弱了。 AFAIK thre 不再是做你想做的事了。

标签: python python-3.x ubuntu service


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-01
    • 2017-08-23
    • 2017-08-01
    • 2010-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多