如何不并行运行批处理文件和python?

2024-10-02 00:36:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我有下面的脚本

(一)

  os.chdir("C:\\RTM3Run_Full\\Python Codes")
  os.startfile("2015_RUNcvm_64_Crash.bat")

(二)

^{pr2}$

我要开始“我在射程内”(设置.loop):“批处理文件完成后的第2部分。但它只并行运行第1)和第2)部分。我知道我应该可以设置一个暂停函数来等待。但我不知道第1部分要花多长时间。有人知道还有其他更聪明的方法来按顺序运行1)和2)吗?在

非常感谢。在


Tags: 文件函数脚本looposcrashfullcodes
3条回答

我的答案是基于这个link

假设在单独的脚本中有这些命令,可以执行以下操作:

import multiprocessing
import os                                              

# Creating the tuple of all the processes which can be run in parallel
all_parallel_processes = ('script_A.py', 'script_B.py', 'script_C.py')     

next_run = ('script_D.py')

# This block of code enables us to call the script from command line.                                                                                
def execute(process):                                                             
    os.system(f'python {process}')                                       


process_pool = Pool(processes = 4)                                                        
process_pool.map(execute, all_parallel_processes)
process_pool.map(execute, next_run)

它们只在脚本之后运行。在

最后我找到了一个简单的解决办法

(一)

在os.chdir公司(“C:\RTM3Run_Full\Python Codes”)

在操作系统(“2015年第64期)_碰撞.bat“)

(二)

我在射程内(设置.loop): .... 在

这保证在第1)部分完成后运行第2)部分。在

~

os.startfile模拟浏览器中的单击。这意味着批处理文件将在后台运行。在

替换此项:

os.chdir("C:\\RTM3Run_Full\\Python Codes")
os.startfile("2015_RUNcvm_64_Crash.bat")

通过适当的阻塞子进程调用,并且由于它是一个批处理文件,并且您希望避免shell=True,前缀为cmd /c,作为参数列表。也不要chdir,只需使用current working directory参数,这样当前目录不会更改:

^{pr2}$

您可能需要检查返回代码,或者在批处理文件返回非零退出代码时使用check_call停止并出现异常。在

相关问题 更多 >

    热门问题