简体   繁体   English

如何从python脚本调用python脚本

[英]How can I call a python script from a python script

I have a python script 'b.py' which prints out time ever 5 sec. 我有一个python脚本'b.py',打印时间为5秒。

while (1):
   print "Start : %s" % time.ctime()
   time.sleep( 5 )
   print "End : %s" % time.ctime()
   time.sleep( 5 )

And in my a.py, I call b.py by: 在我的a.py中,我通过以下方式调用b.py:

def run_b():
        print "Calling run b"
    try:
        cmd = ["./b.py"]

        p = subprocess.Popen(cmd,
                             stdout=subprocess.PIPE,
                             stderr=subprocess.STDOUT)

        for line in iter(p.stdout.readline, b''):
                        print (">>>" + line.rstrip())


    except OSError as e:
        print >>sys.stderr, "fcs Execution failed:", e  

    return None  

and later on, I kill 'b.py' by: PS_PATH = "/usr/bin/ps -efW" 然后,我杀死'b.py':PS_PATH =“/ usr / bin / ps -efW”

def kill_b(program):
    try:

        cmd = shlex.split(PS_PATH)

        retval = subprocess.check_output(cmd).rstrip()
        for line in retval.splitlines():

            if program in line:
                print "line =" + line
                pid = line.split(None)[1]
                os.kill(int(pid), signal.SIGKILL)

    except OSError as e:
        print >>sys.stderr, "kill_all Execution failed:", e
    except subprocess.CalledProcessError as e:
        print >>sys.stderr, "kill_all Execution failed:", e

run_b()
time.sleep(600)
kill_b("b.py")

I have 2 questions. 我有两个问题。 1. why I don't see any prints out from 'b.py' and when I do 'ps -efW' I don't see a process named 'b.py'? 1.为什么我看不到'b.py'中的任何打印件,当我'ps -efW'时,我没有看到名为'b.py'的过程? 2. Why when I kill a process like above, I see 'permission declined'? 2.为什么当我杀死上述过程时,我看到“许可拒绝了”?

I am running above script on cygwin under windows. 我在windows下的cygwin上运行脚本。

Thank you. 谢谢。

  1. Why I don't see any prints out from 'b.py' and when I do 'ps -efW' I don't see a process named 'b.py'? 为什么我从'b.py'看不到任何打印件,当我'ps -efW'时,我没有看到名为'b.py'的进程?

    Change run_b() lines: 更改run_b()行:

     p = subprocess.Popen(cmd, stdout=sys.stdout, stderr=sys.stderr) 

    You will not see a process named "b.py" but something like "python b.py" which is little different. 你不会看到一个名为“b.py”的进程,但是像“python b.py”这样的进程有点不同。 You should use pid instead of name to find it (in your code "p.pid" has the pid). 您应该使用pid而不是name来查找它(在您的代码“p.pid”中有pid)。

  2. Why when I kill a process like above, I see 'permission declined'? 为什么当我杀死上述过程时,我看到“许可拒绝了”?

    os.kill is supported under Windows only 2.7+ and acts a little bit different than posix version. Windows下仅支持os.kill 2.7+,与posix版本略有不同。 However you can use "p.pid". 但是你可以使用“p.pid”。 Best way to kill a process in a cross platform way is: 以跨平台方式终止进程的最佳方法是:

     if platform.system() == "Windows": subprocess.Popen("taskkill /F /T /PID %i" % p.pid, shell=True) else: os.killpg(p.pid, signal.SIGKILL) 

killpg works also on OS X and other Unixy operating systems. killpg也适用于OS X和其他Unixy操作系统。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM