简体   繁体   中英

Subprocess or import to invoke a script in Python

I have a script task.py that I am trying to invoke. It seems there are two ways to do that. One is to use the subprocess API while the other is to use Python's import mechanism.

task.py

def call_task():
    print("task in progress...")
    return "something"

print("calling task..")
out = call_task()
print("output of the executed task::", out)

Now, we have two approaches to invoke the above task.py python script.

Approach 1

import task as task

print("invoke call-task")
out = task.call_task()
print("output::", out)

Approach 2

import subprocess, shlex, PIPE

proc = subprocess.Popen(shlex.split("python task.py"), stdout = PIPE)
out = proc.communicate()
print("output::", out)

Although both approaches work, which approach is more pythonic?

Running a separate Python process from Python is frequently an antipattern. There are situations where you specifically want two Python instances (for example, if the module you want to use requires its own signal handling etc) but in the absence of factors which force the other choice, import is generally vastly preferrable in terms of usability (you get to call the functions inside the package in an order different from its main flow, and have more fine-grained control over the internals) and performance (starting a separate process is almost always a bad idea if you can avoid it).

While "The subprocess module allows you to spawn new processes" which executes the code within your task.py, importing will result in the original process executing your code.

Other than that, it should be identical.

You can read more about it in the Python Subprocess Docs

As i've seen, its rather unusual to execute python code using an extra subprocess.

It may benefit performance wise but the more pythonic way would be importing i guess.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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