【问题标题】:Is there a way to check if script is running from subprocess?有没有办法检查脚本是否从子进程运行?
【发布时间】:2022-06-15 03:20:56
【问题描述】:

我有一个主要的 Python 脚本,它通过 subprocess.Popen 调用另一个脚本:

script1.py:

process = subprocess.Popen(["python", script_path], universal_newlines=True, stdout=subprocess.PIPE)

第二个脚本是一个常规的可运行脚本,但在这种情况下,它是从上面的 scipt 调用的。
在第二个脚本中,我想检查它是否是从子进程调用的。

script2.py:

from_subprocess = True / False # is this possible to check?

if from_subrocess:
    print('some info to pass to the main process')
else:
    pass # no need to print anything

【问题讨论】:

  • 你总是something的子进程。如果您想查找它是什么,那就是PPID 的用途。

标签: python subprocess


猜你喜欢
  • 2017-09-02
  • 2012-10-23
  • 2016-12-06
  • 1970-01-01
  • 2013-01-17
  • 2013-07-23
  • 2022-01-17
  • 2021-10-10
相关资源
最近更新 更多