[英]What is the recommended practice in django to execute external scripts?
I'm planning to build a WebApp that will need to execute scripts based on the argument that an user will provide in a text-field or in the Url. 我打算构建一个WebApp,它将需要根据用户将在文本字段或Url中提供的参数来执行脚本。
possible solutions that I have found: 我发现的可能解决方案:
create a lib directory in the root directory of the project, and put the scripts there, and import it from views. 在项目的根目录中创建一个lib目录,并将脚本放在此处,然后从视图导入它。
using subprocess module to directly run the scripts in the following way: 使用子流程模块通过以下方式直接运行脚本:
subprocess.call(['python', 'somescript.py', argument_1,...])
argument_1: should be what an end user provides. arguments_1:应该是最终用户提供的内容。
I'm planning to build a WebApp that will need to execute scripts
我打算构建一个需要执行脚本的WebApp
Why should it "execute scripts" ? 为什么要“执行脚本”? Turn your "scripts" into proper modules , import the relevant functions and call them.
将您的“脚本”转换为适当的模块 ,导入相关功能并进行调用。 The fact that Python can be used as a "scripting language" doesn't mean it's not a proper programming language.
Python 可以用作“脚本语言”的事实并不意味着它不是适当的编程语言。
Approach (1) should be the default approach. 方法(1)应该是默认方法。 Never subprocess unless you absolutely have to.
除非绝对必要,否则请勿进行子处理。
Disadvantages of subprocessing: 子处理的缺点:
python
command the same as the Python that runs the original script?). python
命令与运行原始脚本的Python是否相同?)。 Advantages of subprocessing: 子处理的优点:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.