sliders_alpha
sliders_alpha

Reputation: 2454

python function appear to not be called, how is this possible?

I have this script, it's purpose is to call an other script while with different parameters and print the output as it would be print if I called it myself :

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")

however the output is this :

iteration
iteration
done

Process finished with exit code 0

how is this possible? there is a print at the start of the run_this() function.

Thanks.

ps : you can pass any command to run_this(), it will never go into the function. for example, this will never print 'running toto'

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")

Upvotes: 0

Views: 60

Answers (1)

user2357112
user2357112

Reputation: 281401

Your run_this is a generator function. Calling it doesn't actually run anything. It just creates a generator iterator. Iterating over the iterator would run the code.

Upvotes: 3

Related Questions