我有这个脚本,它的目的是用不同的参数调用另一个脚本,并打印输出,就像我自己调用它一样:
import subprocess
def run_this(command):
print(f"running {command}")
p = subprocess.Popen(command.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
while True:
retcode = p.poll()
line = p.stdout.readline()
if line:
yield line
if retcode is not None:
print(f"retcode : {retcode}")
p.stdout.close()
break
def build_command(pruned_model, prompt):
return f'python scripts/stable_txt2img.py --ddim_eta 0.0 --n_samples 1 --n_iter 4 --scale 7.0 ' \
+ f'--ddim_steps 50 --ckpt "{pruned_model}" ' \
+ f'--prompt "{prompt}" --seed 6514689'
pruned_model = r"C:\checkout2\Stable-diffusion\checkpoints\last-pruned.ckpt"
prompts = [
"a person in space",
"a person on a boat"
]
for prompt in prompts:
print("iteration")
command = build_command(pruned_model, prompt)
run_this(command)
print("done")然而,产出如下:
iteration
iteration
done
Process finished with exit code 0这怎麽可能?在run_this()函数的开头有一个打印。
谢谢。
ps :您可以将任何命令传递给run_this(),它将永远不会进入函数。例如,这将永远不会打印“running”
import subprocess
def run_this(command):
print(f"running {command}")
p = subprocess.Popen(command.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
while True:
retcode = p.poll()
line = p.stdout.readline()
if line:
yield line
if retcode is not None:
print(f"retcode : {retcode}")
p.stdout.close()
break
print("start")
run_this("toto")
print("done")发布于 2022-10-01 06:25:42
您的run_this是一个生成器函数。打电话实际上不运行任何东西。它只是创建了一个生成器迭代器。迭代器上的迭代将运行代码。
https://stackoverflow.com/questions/73916206
复制相似问题