简体   繁体   English

如何使子进程在python中可见

[英]How to make subprocess visible in python

I have a python script that does 2 things sequentially: 我有一个python脚本,可以依次执行2件事:

  1. generate a bunch of files and then 生成一堆文件,然后
  2. move files around. 移动文件。

I'm moving the files by iterating through all the files in the folder with: 我通过以下方式遍历文件夹中的所有文件来移动文件:

for filename in os.listdir("."):
    if filename.endswith(".rmp"):

where the .rmp files are exactly the ones generated by the first subprocess. .rmp文件正是第一个子进程生成的文件。
So right now running it once only gets the .rmp files in the folder and the second step do not see them for whatever reason, but it moves them correctly on second running as expected. 因此,现在运行它一次仅将.rmp文件获取到文件夹中,并且第二步由于某种原因看不到它们,但是它将按预期在第二次运行时正确地移动它们。
So how do I get it to recognize all the files in the very first try. 因此,如何在第一次尝试中识别所有文件。 Why are they not visible in the first place? 为什么它们首先不可见?

here's the code 这是代码

var = os.getcwd()
pipe = subprocess.Popen(["perl", "./runtest.pl", var])


for filename in os.listdir("."):
    if filename.endswith(".rmp"):
        print "woopee"

I think you're not closing the files. 我认为您没有关闭文件。 That might delay a write and cause os.listdir not to get all the files. 这可能会延迟写入,并导致os.listdir无法获取所有文件。

One nice way to ensure you're always closing the files is with: 确保始终关闭文件的一种好方法是:

with open('filename.rmp', 'w') as f:
  f.write(...)

This is nice because it works well with exceptions so that you're sure you don't forget them opened. 很好,因为它可以很好地处理异常,以确保您不会忘记打开它们。

Once everything is closed they should show up in os.listdir(.) 一旦一切都关闭,它们应该显示在os.listdir(.)

When you create a subprocess, it executes in parallel with your current process. 创建子流程时,它与当前流程并行执行。 Since your process continues running while your perl script runs, it's a race between your program reading the directory and the perl process writing its files (this is your race condition ). 由于您的进程在perl脚本运行时继续运行,因此这是程序在读取目录与perl进程在写入文件之间进行竞争 (这是您的竞争条件 )。

To resolve it, you should add a call to .wait() : 要解决它,您应该添加一个对.wait()的调用:

var = os.getcwd()
p = subprocess.Popen(["perl", "./runtest.pl", var])
p.wait() # wait for perl to exit

for filename in os.listdir("."):
    if filename.endswith(".rmp"):
        print "woopee"

I'd also add that this would be a good opportunity to use the glob module: 我还要补充一点,这是使用glob模块的好机会:

from glob import glob
var = os.getcwd()
p = subprocess.Popen(["perl", "./runtest.pl", var])
p.wait()

for filename in glob('*.rpm'):
    print "woopee"

Looks like you don't need any communication with the subprocess, all you need is just run a subprocess and wait for exit. 看起来您不需要与子流程进行任何通信,您只需要运行一个子流程并等待退出即可。 subprocess.call() is more convenient: subprocess.call()更方便:

var = os.getcwd()
retcode = subprocess.call(["perl", "./runtest.pl", var])
if retcode != 0:
    # do some error handling, the subprocess returned nonzero exit code

# process the file generated by subprocess

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

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