Async execute cannot reuse already computed results
If there is another await KSampler() between a KSampler() and a VAEDecode(), like this:
from comfy_script.runtime import *
load(watch=False)
queue.start_watch(False,False,False)
from comfy_script.runtime.nodes import *
...
latent1 = KSampler() # generate one image
await DummyOut(latent1) # using DummyOut() from KJNodes package to create an output node.
latent2 = KSampler()) # generate another image using different prompt
await DummyOut(latent2)
image1 = VAEDecode(latent1, vae)
The latent1 KSampler() will run twice. If the latent2 KSampler() removed, the latent1 KSampler will run only once, which is expected behavior,
This is actually because you cannot get intermediate output directly from the workflow. See https://github.com/Chaoses-Ib/ComfyScript/blob/a54e894e4d33b899f3663e6f12bba71c7241411a/docs/Runtime.md#virtual-mode:
The main limitation of virtual mode is that one cannot get the output of nodes from Python before running the full workflow. However, this limitation can be mitigated by expanding a workflow dynamically and run it multiple times. See select and process for an example. (If
#931is someday merged into ComfyUI, this limitation can be solved.)Another solution to the above limitation is to use real mode.
There is a plan to improve this with workflow expansion, but it's a bit complex and I haven't done it yet: https://github.com/Chaoses-Ib/ComfyScript/issues/29#issuecomment-1975270522
OK. Implementation of #29 solved the issue through util.get_image() which will download images to client side, expensive. I think maintain all intermediate results on server side and removing them on demand is more flexible, but comfyUI may not support this althrough #2226 is merged. I'll use real mode then, the most flexible.
through util.get_image() which will download images to client side, expensive.
You can also directly read the image file instead, by util.save_image_and_get_paths(image, temp = True): https://github.com/Chaoses-Ib/ComfyScript/blob/a54e894e4d33b899f3663e6f12bba71c7241411a/src/comfy_script/runtime/util/_impl.py#L129-L163
It's still a bit slower than real mode, but there is probably no real difference for the user.
I think maintain all intermediate results on server side and removing them on demand is more flexible, but comfyUI may not support this althrough #2226 is merged.
Although ComfyUI doesn't support this, there is actually some hacky ways to do it. One can keep all executed nodes in the new workflow, with the same old ID, so all the results will be kept. But its use case looks very limited, i.e. only when executing workflows alternatively.
Just make your two workflows into one big workflow by with Workflow() is probably more practical. You can get the two output images after the one big workflow is done.