简体   繁体   English

如何在Python中通过“无命令行活动”来触发下一个软件包的安装

[英]How to trigger next package installation by “no command line activity” in Python

I build installation wizard to application in Python. 我在Python中为应用程序构建安装向导。 Recognising all commands included - requires, prior to running - installing of about 20 different packages (project uses different calculation types, like: SVM, FFT, 3D harmonics, K-nearest neighbours, and additional packages for utilising command line and for GUI). 识别包括的所有命令-在运行之前需要-安装大约20个不同的软件包(项目使用不同的计算类型,例如:SVM,FFT,3D谐波,K近邻,以及用于命令行和GUI的其他软件包)。

All packages - are needed to be installed one-after-another, sequentially (one installation finishes, then I want to start next package installation). 所有软件包-需要一个接一个地,依次安装(一个安装完成,然后我要开始下一个软件包安装)。 During installation - there are different print status indications, which are automatically printed by installation (not me). 在安装过程中-有不同的打印状态指示,安装时会自动打印(不是我)。

Eventually - all command line prints stop, and this "lack of activity of CMD" - I want to become trigger to run (by my project) next command for installation of next Python package. 最终-所有命令行打印停止,而这种“ CMD活动不足”-我想成为触发器(由我的项目运行)以安装下一个Python软件包的下一个命令。 I think I may somehow use "stdout" emptiness, or other system entity. 我想我可能会以某种方式使用“ stdout”空度或其他系统实体。 Please tell how to implement it, or provide short example, or link to example. 请说明如何实现,或提供简短示例,或链接到示例。 Thanks in advance. 提前致谢。

The default state of most things in programming is to wait for a function/method/process to return before moving on. 编程中大多数事情的默认状态是等待函数/方法/过程返回后再继续。 For example, if you use subprocess.run() to run each command involved in installing, it will wait for the process to return before doing the next thing. 例如,如果使用subprocess.run()运行安装中涉及的每个命令,它将在执行下一步之前等待进程返回。 The same goes for most other ways of installing. 大多数其他安装方式也是如此。

In general, unless you explicitly use some form of concurrency (or something else that uses such, which will almost certainly say so in the documentation), it's going to wait. 通常,除非您显式使用某种形式的并发 (或其他使用这种形式的并发 ,在文档中几乎肯定会这样说),否则它将等待。

So if your install commands are stored in variables cmd1 , cmd2 , etc, and none take any input once started: 因此,如果您的安装命令存储在cmd1cmd2等变量中,并且启动后没有任何输入:

# some code
subprocess.run(cmd1, stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=True)
subprocess.run(cmd2, stdout=subprocess.PIPE,  stderr=subprocess.PIPE, check=True)
# whatever follows that

will run cmd1 , then run cmd2 when cmd1 finishes, putting the stdout output of each to the existing stdout , and raising a CalledProcessError and stopping if either one returns non-zero. 将运行cmd1 ,然后在cmd1完成时运行cmd2 ,将每个的stdout输出放到现有的stdout ,并引发CalledProcessError并在任何一个返回非零值时停止。

Note that if you were to watch stdout instead, the result would probably break if anything was slow - a long blank period without actually being done. 请注意,如果您改为观看stdout ,则如果速度较慢,则结果可能会中断-较长的空白期而实际上并未完成。

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

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