Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python脚本启动新的Python脚本_Python_Python 3.x - Fatal编程技术网

Python脚本启动新的Python脚本

Python脚本启动新的Python脚本,python,python-3.x,Python,Python 3.x,我有一个脚本“run.py”,它必须打印“Hello”,启动另一个脚本“run2.py”,然后终止(不要等待run2.py返回)。 run2.py不在本地目录中,只需要打印“Hello Reach” 我该怎么做 # run_path = "C:/Program Files (x86)/xxx/run.py" # run2_path = "//network_share/folder/run2.py" **run.py** import os print("Hello") # What do

我有一个脚本“run.py”,它必须打印“Hello”,启动另一个脚本“run2.py”,然后终止(不要等待run2.py返回)。 run2.py不在本地目录中,只需要打印“Hello Reach”

我该怎么做

# run_path = "C:/Program Files (x86)/xxx/run.py"
# run2_path = "//network_share/folder/run2.py"

**run.py**
import os

print("Hello")

# What do I do here?
# os.execl("//network_share/folder/run2.py")

exit()


**run2.py**
print("Hello again")

子流程是您的朋友,但如果您不需要等待,请查看p_NOWAIT——替换中的示例代码

例如: pid=Popen([“/bin/mycmd”,“myarg]”)。pid


我不认为。这一次你需要的是交流——不是更需要等待吗?

子流程是你的朋友,但如果你不需要等待,请查看p_NOWAIT——替换中的示例代码

例如: pid=Popen([“/bin/mycmd”,“myarg]”)。pid


我不这么认为。这一次你需要的是交流,不是更需要等待吗?

最干净的方法是(因为两个脚本都是用纯Python编写的)将另一个脚本作为模块导入并执行其内容,放在函数中:

run.py

import os
import sys
sys.path.append("//network_share/folder/")

import run2 
print("Hello")

run2.main()
exit()
def main():
    print("Hello again")
run2.py

import os
import sys
sys.path.append("//network_share/folder/")

import run2 
print("Hello")

run2.main()
exit()
def main():
    print("Hello again")

最干净的方法是(因为这两个脚本都是用纯Python编写的)将另一个脚本作为模块导入并执行其内容,放在函数中:

run.py

import os
import sys
sys.path.append("//network_share/folder/")

import run2 
print("Hello")

run2.main()
exit()
def main():
    print("Hello again")
run2.py

import os
import sys
sys.path.append("//network_share/folder/")

import run2 
print("Hello")

run2.main()
exit()
def main():
    print("Hello again")

这似乎适用于我正在运行此脚本的同一文件夹中的脚本

这应该验证第一个脚本是否已完成,并且在第二个脚本在其自己的进程中运行时不会延迟。在某些系统上,由于其配置,子进程可能会在父进程终止时终止。但不是在这种情况下

我在这篇文章中花了更多的时间来添加代码,说明如何检查父进程是否仍在运行。这将是孩子确保退出的好方法。还显示了如何将参数传递给子进程

# launch.py
import subprocess as sp
import os

if __name__ == '__main__':

    sp.Popen(['ps']) # Print out runniing processes.

    print("launch.py's process id is %s." % os.getpid())

    # Give child process this one's process ID in the parameters.

    sp.Popen(['python3', 'runinproc.py', str(os.getpid())])

    # ^^^ This line above anwers the main question of how to kick off a
    #     child Python script.

    print("exiting launch.py")
其他脚本

# runinproc.py
import time
import subprocess as sp
import sys
import os

def is_launcher_running():
    try:
        # This only checks the status of the process. It doesn't 
        # kill it, or otherwise affect it.

        os.kill(int(sys.argv[1]), 0)

    except OSError:
        return False
    else:
        return True

if __name__ == '__main__':

    print("runinproc.py was launched by process ID %s" % sys.argv[1])

    for i in range(100):

        if is_launcher_running():
            # Is launch.py still running?
            print("[[ launch.py is still running... ]]")

        sp.Popen(['ps']) # Print out the running processes.

        print("going to sleep for 2 seconds...")

        time.sleep(2)
Bash输出:

Todds-iMac:pyexperiments todd$ python3 launch.py
launch.py process id is 40975.
exiting launch.py
Todds-iMac:pyexperiments todd$ runinproc.py was launched by process ID 40975
going to sleep for 2 seconds...
  PID TTY           TIME CMD
  PID TTY           TIME CMD
40866 ttys000    0:00.09 -bash
40866 ttys000    0:00.09 -bash
40977 ttys000    0:00.04 /Library/Frameworks/Python.framework/Versions/3.8/Resources/Python.app/C
40977 ttys000    0:00.04 /Library/Frameworks/Python.framework/Versions/3.8/Resources/Python.app/C
going to sleep for 2 seconds...
  PID TTY           TIME CMD
40866 ttys000    0:00.09 -bash
40977 ttys000    0:00.04 /Library/Frameworks/Python.framework/Versions/3.8/Resources/Python.app/C
going to sleep for 2 seconds...
  PID TTY           TIME CMD
40866 ttys000    0:00.09 -bash
40977 ttys000    0:00.04 /Library/Frameworks/Python.framework/Versions/3.8/Resources/Python.app/C
going to sleep for 2 seconds...

请注意,对shell的第一个调用&launch.py的
ps
是在launch.py退出后执行的。这就是为什么它没有显示在打印的进程列表中。

这似乎适用于我运行此脚本的同一文件夹中的脚本

这应该验证第一个脚本是否已完成,并且在第二个脚本在其自己的进程中运行时不会延迟。在某些系统上,由于其配置,子进程可能会在父进程终止时终止。但不是在这种情况下

我在这篇文章中花了更多的时间来添加代码,说明如何检查父进程是否仍在运行。这将是孩子确保退出的好方法。还显示了如何将参数传递给子进程

# launch.py
import subprocess as sp
import os

if __name__ == '__main__':

    sp.Popen(['ps']) # Print out runniing processes.

    print("launch.py's process id is %s." % os.getpid())

    # Give child process this one's process ID in the parameters.

    sp.Popen(['python3', 'runinproc.py', str(os.getpid())])

    # ^^^ This line above anwers the main question of how to kick off a
    #     child Python script.

    print("exiting launch.py")
其他脚本

# runinproc.py
import time
import subprocess as sp
import sys
import os

def is_launcher_running():
    try:
        # This only checks the status of the process. It doesn't 
        # kill it, or otherwise affect it.

        os.kill(int(sys.argv[1]), 0)

    except OSError:
        return False
    else:
        return True

if __name__ == '__main__':

    print("runinproc.py was launched by process ID %s" % sys.argv[1])

    for i in range(100):

        if is_launcher_running():
            # Is launch.py still running?
            print("[[ launch.py is still running... ]]")

        sp.Popen(['ps']) # Print out the running processes.

        print("going to sleep for 2 seconds...")

        time.sleep(2)
Bash输出:

Todds-iMac:pyexperiments todd$ python3 launch.py
launch.py process id is 40975.
exiting launch.py
Todds-iMac:pyexperiments todd$ runinproc.py was launched by process ID 40975
going to sleep for 2 seconds...
  PID TTY           TIME CMD
  PID TTY           TIME CMD
40866 ttys000    0:00.09 -bash
40866 ttys000    0:00.09 -bash
40977 ttys000    0:00.04 /Library/Frameworks/Python.framework/Versions/3.8/Resources/Python.app/C
40977 ttys000    0:00.04 /Library/Frameworks/Python.framework/Versions/3.8/Resources/Python.app/C
going to sleep for 2 seconds...
  PID TTY           TIME CMD
40866 ttys000    0:00.09 -bash
40977 ttys000    0:00.04 /Library/Frameworks/Python.framework/Versions/3.8/Resources/Python.app/C
going to sleep for 2 seconds...
  PID TTY           TIME CMD
40866 ttys000    0:00.09 -bash
40977 ttys000    0:00.04 /Library/Frameworks/Python.framework/Versions/3.8/Resources/Python.app/C
going to sleep for 2 seconds...


请注意,对shell的第一个调用&launch.py的
ps
是在launch.py退出后执行的。这就是为什么它没有显示在打印的进程列表中。

子进程
是一个很好的python包,应该可以帮助您做到这一点:您应该看看
Popen.communicate
方法。@baptiste.communicate等待子进程终止。我需要在run2.py printsry之前使用
runpy
模块终止run.py,或者读取“run2.py”并使用
exec
函数退出当前脚本后无法运行命令。至少不是脚本本身。@Pitto完全正确!我正在尝试在退出run.py之前启动run2.py。。。。run.py需要在调用run2.py后立即退出。。我可能会在打印之前添加一个延迟(“再次您好”),以确保run.py被关闭
子进程
是一个很好的python包,应该可以帮助您做到这一点:您应该查看
Popen.communicate
方法。@baptiste.communicate等待子进程终止。我需要在run2.py printsry之前使用
runpy
模块终止run.py,或者读取“run2.py”并使用
exec
函数退出当前脚本后无法运行命令。至少不是脚本本身。@Pitto完全正确!我正在尝试在退出run.py之前启动run2.py。。。。run.py需要在调用run2.py后立即退出。。我可能会在打印之前添加一个延迟(“再次您好”),以确保run.py关闭
sys。path
告诉Python解释器在哪里查找模块。此方法不会在打印run2.py的内容之前终止run.py。我需要在“再见”之前杀了run.pyprinted@CodeGod我想知道为什么这是必要的。你能不能对你的问题多看一眼?我真正想做的是让脚本“run2.py”充当“run.py”的更新程序脚本。“run2.py”从网络共享复制更新版本的“run.py”,替换本地版本的run.py,然后启动它。因此,为什么需要终止run.py(如果run2.py仍在运行,则无法替换run.py)。@codegood那么,最好执行run.py的一个副本,而不是它本身。这将解除脚本的更改。
sys.path
告诉Python解释器在何处查找模块。此方法不会在打印run2.py的内容之前终止run.py。我需要在“再见”之前杀了run.pyprinted@CodeGod我想知道为什么这是必要的。你能不能对你的问题多看一眼?我真正想做的是让脚本“run2.py”充当“run.py”的更新程序脚本。“run2.py”从网络共享复制更新版本的“run.py”,替换本地版本的run.py,然后启动它。因此,为什么需要终止run.py(如果run2.py仍在运行,则无法替换run.py)。@codegood那么,最好执行run.py的一个副本,而不是它本身。我不得不将“python3”改为“python”,它的效果非常好!谢谢你,所以muchI不得不将“python3”改为“python”,而且效果非常好!多谢各位