确定python脚本是否作为子进程调用并传递参数

确定python脚本是否作为子进程调用并传递参数,python,subprocess,Python,Subprocess,我有一个脚本,它提供了在完成后运行第二个脚本的选项。我想知道第二个脚本是单独运行还是作为子进程运行的,是否有一个好方法。如果它作为子进程调用,则将args传递到第二个脚本中 第一个脚本的结尾如下: dlg = wx.MessageDialog(None, "Shall we format?",'Format Files',wx.YES_NO | wx.ICON_QUESTION) result = dlg.ShowModal() if result == wx.ID_YES: call

我有一个脚本,它提供了在完成后运行第二个脚本的选项。我想知道第二个脚本是单独运行还是作为子进程运行的,是否有一个好方法。如果它作为子进程调用,则将args传递到第二个脚本中

第一个脚本的结尾如下:

dlg = wx.MessageDialog(None, "Shall we format?",'Format Files',wx.YES_NO | wx.ICON_QUESTION)
result = dlg.ShowModal()

if result == wx.ID_YES:
    call("Threading.py", shell=True) 
else:
    pass

第二个脚本是一个独立的脚本,它接受3个文件并将它们格式化为一个。args将只在第二个脚本中设置文件名。

因此我将使用os.getppid检索父进程pid,然后使用Popen将其作为参数传递给子进程:

parent.py

child.py

所以当你从父母那里打电话给孩子的时候

$ ./parent.py 
Parent is 72297 child is 72346
此时很容易进行比较和检查pid

#!/usr/bin/env python

import sys
import os

parent_pid = sys.argv[1]
my_pid = str(os.getppid())

print "Parent is %s child is %s " % ( parent_pid, my_pid )
$ ./parent.py 
Parent is 72297 child is 72346