mirror of
https://git.datalinker.icu/comfyanonymous/ComfyUI
synced 2026-11-20 11:07:02 +08:00
Initial infrastructure for synchronous results
This commit is contained in:
parent
febea8c101
commit
3c5d5445e3
36
execution.py
36
execution.py
@ -26,7 +26,7 @@ def get_input_data(inputs, class_def, unique_id, outputs={}, prompt={}, extra_da
|
|||||||
obj = outputs[input_unique_id][output_index]
|
obj = outputs[input_unique_id][output_index]
|
||||||
input_data_all[x] = obj
|
input_data_all[x] = obj
|
||||||
else:
|
else:
|
||||||
if ("required" in valid_inputs and x in valid_inputs["required"]) or ("optional" in valid_inputs and x in valid_inputs["optional"]):
|
if ("required" in valid_inputs and x in valid_inputs["required"]) or ("optional" in valid_inputs and x in valid_inputs["optional"]) or ("hidden" in valid_inputs and x in valid_inputs["hidden"]):
|
||||||
input_data_all[x] = [input_data]
|
input_data_all[x] = [input_data]
|
||||||
|
|
||||||
if "hidden" in valid_inputs:
|
if "hidden" in valid_inputs:
|
||||||
@ -72,6 +72,7 @@ def get_output_data(obj, input_data_all):
|
|||||||
|
|
||||||
results = []
|
results = []
|
||||||
uis = []
|
uis = []
|
||||||
|
sync_results = []
|
||||||
return_values = map_node_over_list(obj, input_data_all, obj.FUNCTION, allow_interrupt=True)
|
return_values = map_node_over_list(obj, input_data_all, obj.FUNCTION, allow_interrupt=True)
|
||||||
|
|
||||||
for r in return_values:
|
for r in return_values:
|
||||||
@ -80,6 +81,8 @@ def get_output_data(obj, input_data_all):
|
|||||||
uis.append(r['ui'])
|
uis.append(r['ui'])
|
||||||
if 'result' in r:
|
if 'result' in r:
|
||||||
results.append(r['result'])
|
results.append(r['result'])
|
||||||
|
if 'sync_result' in r:
|
||||||
|
sync_results.append(r['sync_result'])
|
||||||
else:
|
else:
|
||||||
results.append(r)
|
results.append(r)
|
||||||
|
|
||||||
@ -100,7 +103,7 @@ def get_output_data(obj, input_data_all):
|
|||||||
ui = dict()
|
ui = dict()
|
||||||
if len(uis) > 0:
|
if len(uis) > 0:
|
||||||
ui = {k: [y for x in uis for y in x[k]] for k in uis[0].keys()}
|
ui = {k: [y for x in uis for y in x[k]] for k in uis[0].keys()}
|
||||||
return output, ui
|
return output, ui, sync_results
|
||||||
|
|
||||||
def format_value(x):
|
def format_value(x):
|
||||||
if x is None:
|
if x is None:
|
||||||
@ -110,7 +113,7 @@ def format_value(x):
|
|||||||
else:
|
else:
|
||||||
return str(x)
|
return str(x)
|
||||||
|
|
||||||
def recursive_execute(server, prompt, outputs, current_item, extra_data, executed, prompt_id, outputs_ui, object_storage):
|
def recursive_execute(server, prompt, outputs, current_item, extra_data, executed, prompt_id, outputs_ui, outputs_sync, object_storage):
|
||||||
unique_id = current_item
|
unique_id = current_item
|
||||||
inputs = prompt[unique_id]['inputs']
|
inputs = prompt[unique_id]['inputs']
|
||||||
class_type = prompt[unique_id]['class_type']
|
class_type = prompt[unique_id]['class_type']
|
||||||
@ -125,7 +128,7 @@ def recursive_execute(server, prompt, outputs, current_item, extra_data, execute
|
|||||||
input_unique_id = input_data[0]
|
input_unique_id = input_data[0]
|
||||||
output_index = input_data[1]
|
output_index = input_data[1]
|
||||||
if input_unique_id not in outputs:
|
if input_unique_id not in outputs:
|
||||||
result = recursive_execute(server, prompt, outputs, input_unique_id, extra_data, executed, prompt_id, outputs_ui, object_storage)
|
result = recursive_execute(server, prompt, outputs, input_unique_id, extra_data, executed, prompt_id, outputs_ui, outputs_sync, object_storage)
|
||||||
if result[0] is not True:
|
if result[0] is not True:
|
||||||
# Another node failed further upstream
|
# Another node failed further upstream
|
||||||
return result
|
return result
|
||||||
@ -142,12 +145,14 @@ def recursive_execute(server, prompt, outputs, current_item, extra_data, execute
|
|||||||
obj = class_def()
|
obj = class_def()
|
||||||
object_storage[(unique_id, class_type)] = obj
|
object_storage[(unique_id, class_type)] = obj
|
||||||
|
|
||||||
output_data, output_ui = get_output_data(obj, input_data_all)
|
output_data, output_ui, output_sync = get_output_data(obj, input_data_all)
|
||||||
outputs[unique_id] = output_data
|
outputs[unique_id] = output_data
|
||||||
if len(output_ui) > 0:
|
if len(output_ui) > 0:
|
||||||
outputs_ui[unique_id] = output_ui
|
outputs_ui[unique_id] = output_ui
|
||||||
if server.client_id is not None:
|
if server.client_id is not None:
|
||||||
server.send_sync("executed", { "node": unique_id, "output": output_ui, "prompt_id": prompt_id }, server.client_id)
|
server.send_sync("executed", { "node": unique_id, "output": output_ui, "prompt_id": prompt_id }, server.client_id)
|
||||||
|
if len(output_sync) > 0:
|
||||||
|
outputs_sync[unique_id] = output_sync
|
||||||
except comfy.model_management.InterruptProcessingException as iex:
|
except comfy.model_management.InterruptProcessingException as iex:
|
||||||
print("Processing interrupted")
|
print("Processing interrupted")
|
||||||
|
|
||||||
@ -262,6 +267,7 @@ class PromptExecutor:
|
|||||||
self.outputs = {}
|
self.outputs = {}
|
||||||
self.object_storage = {}
|
self.object_storage = {}
|
||||||
self.outputs_ui = {}
|
self.outputs_ui = {}
|
||||||
|
self.outputs_sync = {}
|
||||||
self.old_prompt = {}
|
self.old_prompt = {}
|
||||||
self.server = server
|
self.server = server
|
||||||
|
|
||||||
@ -347,6 +353,10 @@ class PromptExecutor:
|
|||||||
if x not in current_outputs:
|
if x not in current_outputs:
|
||||||
d = self.outputs_ui.pop(x)
|
d = self.outputs_ui.pop(x)
|
||||||
del d
|
del d
|
||||||
|
for x in list(self.outputs_sync.keys()):
|
||||||
|
if x not in current_outputs:
|
||||||
|
d = self.outputs_sync.pop(x)
|
||||||
|
del d
|
||||||
|
|
||||||
if self.server.client_id is not None:
|
if self.server.client_id is not None:
|
||||||
self.server.send_sync("execution_cached", { "nodes": list(current_outputs) , "prompt_id": prompt_id}, self.server.client_id)
|
self.server.send_sync("execution_cached", { "nodes": list(current_outputs) , "prompt_id": prompt_id}, self.server.client_id)
|
||||||
@ -365,7 +375,7 @@ class PromptExecutor:
|
|||||||
# This call shouldn't raise anything if there's an error deep in
|
# This call shouldn't raise anything if there's an error deep in
|
||||||
# the actual SD code, instead it will report the node where the
|
# the actual SD code, instead it will report the node where the
|
||||||
# error was raised
|
# error was raised
|
||||||
success, error, ex = recursive_execute(self.server, prompt, self.outputs, output_node_id, extra_data, executed, prompt_id, self.outputs_ui, self.object_storage)
|
success, error, ex = recursive_execute(self.server, prompt, self.outputs, output_node_id, extra_data, executed, prompt_id, self.outputs_ui, self.outputs_sync, self.object_storage)
|
||||||
if success is not True:
|
if success is not True:
|
||||||
self.handle_execution_error(prompt_id, prompt, current_outputs, executed, error, ex)
|
self.handle_execution_error(prompt_id, prompt, current_outputs, executed, error, ex)
|
||||||
break
|
break
|
||||||
@ -682,6 +692,7 @@ class PromptQueue:
|
|||||||
self.queue = []
|
self.queue = []
|
||||||
self.currently_running = {}
|
self.currently_running = {}
|
||||||
self.history = {}
|
self.history = {}
|
||||||
|
self.futures = {}
|
||||||
server.prompt_queue = self
|
server.prompt_queue = self
|
||||||
|
|
||||||
def put(self, item):
|
def put(self, item):
|
||||||
@ -696,18 +707,25 @@ class PromptQueue:
|
|||||||
self.not_empty.wait()
|
self.not_empty.wait()
|
||||||
item = heapq.heappop(self.queue)
|
item = heapq.heappop(self.queue)
|
||||||
i = self.task_counter
|
i = self.task_counter
|
||||||
|
if len(item) > 5:
|
||||||
|
future = item[5]
|
||||||
|
item = item[:5]
|
||||||
|
self.futures[i] = future
|
||||||
self.currently_running[i] = copy.deepcopy(item)
|
self.currently_running[i] = copy.deepcopy(item)
|
||||||
self.task_counter += 1
|
self.task_counter += 1
|
||||||
self.server.queue_updated()
|
self.server.queue_updated()
|
||||||
return (item, i)
|
return (item, i)
|
||||||
|
|
||||||
def task_done(self, item_id, outputs):
|
def task_done(self, item_id, outputs_ui, outputs_sync):
|
||||||
with self.mutex:
|
with self.mutex:
|
||||||
prompt = self.currently_running.pop(item_id)
|
prompt = self.currently_running.pop(item_id)
|
||||||
self.history[prompt[1]] = { "prompt": prompt, "outputs": {} }
|
self.history[prompt[1]] = { "prompt": prompt, "outputs": {} }
|
||||||
for o in outputs:
|
for o in outputs_ui:
|
||||||
self.history[prompt[1]]["outputs"][o] = outputs[o]
|
self.history[prompt[1]]["outputs"][o] = outputs_ui[o]
|
||||||
self.server.queue_updated()
|
self.server.queue_updated()
|
||||||
|
if item_id in self.futures:
|
||||||
|
self.futures[item_id].set_result(outputs_sync)
|
||||||
|
del self.futures[item_id]
|
||||||
|
|
||||||
def get_current_queue(self):
|
def get_current_queue(self):
|
||||||
with self.mutex:
|
with self.mutex:
|
||||||
|
|||||||
2
main.py
2
main.py
@ -35,7 +35,7 @@ def prompt_worker(q, server):
|
|||||||
execution_start_time = time.perf_counter()
|
execution_start_time = time.perf_counter()
|
||||||
prompt_id = item[1]
|
prompt_id = item[1]
|
||||||
e.execute(item[2], prompt_id, item[3], item[4])
|
e.execute(item[2], prompt_id, item[3], item[4])
|
||||||
q.task_done(item_id, e.outputs_ui)
|
q.task_done(item_id, e.outputs_ui, e.outputs_sync)
|
||||||
if server.client_id is not None:
|
if server.client_id is not None:
|
||||||
server.send_sync("executing", { "node": None, "prompt_id": prompt_id }, server.client_id)
|
server.send_sync("executing", { "node": None, "prompt_id": prompt_id }, server.client_id)
|
||||||
|
|
||||||
|
|||||||
61
server.py
61
server.py
@ -10,6 +10,7 @@ import glob
|
|||||||
import struct
|
import struct
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
|
import copy
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import aiohttp
|
import aiohttp
|
||||||
@ -451,6 +452,64 @@ class PromptServer():
|
|||||||
else:
|
else:
|
||||||
return web.json_response({"error": "no prompt", "node_errors": []}, status=400)
|
return web.json_response({"error": "no prompt", "node_errors": []}, status=400)
|
||||||
|
|
||||||
|
def merge_dict_recursive(dict1, dict2):
|
||||||
|
"""
|
||||||
|
Recursively merges two dictionaries, concatenating arrays instead of overwriting them.
|
||||||
|
"""
|
||||||
|
for key in dict2:
|
||||||
|
if key in dict1:
|
||||||
|
if isinstance(dict1[key], dict) and isinstance(dict2[key], dict):
|
||||||
|
merge_dict_recursive(dict1[key], dict2[key])
|
||||||
|
elif isinstance(dict1[key], list) and isinstance(dict2[key], list):
|
||||||
|
dict1[key].extend(dict2[key])
|
||||||
|
else:
|
||||||
|
dict1[key] = dict2[key]
|
||||||
|
else:
|
||||||
|
dict1[key] = dict2[key]
|
||||||
|
return dict1
|
||||||
|
|
||||||
|
@routes.post("/prompt_sync")
|
||||||
|
async def post_prompt_sync(request):
|
||||||
|
print("got prompt_sync")
|
||||||
|
resp_code = 200
|
||||||
|
out_string = ""
|
||||||
|
json_data = await request.json()
|
||||||
|
|
||||||
|
if "number" in json_data:
|
||||||
|
number = float(json_data['number'])
|
||||||
|
else:
|
||||||
|
number = self.number
|
||||||
|
if "front" in json_data:
|
||||||
|
if json_data['front']:
|
||||||
|
number = -number
|
||||||
|
|
||||||
|
self.number += 1
|
||||||
|
|
||||||
|
if "prompt" in json_data:
|
||||||
|
prompt = json_data["prompt"]
|
||||||
|
valid = execution.validate_prompt(prompt)
|
||||||
|
extra_data = {}
|
||||||
|
if "extra_data" in json_data:
|
||||||
|
extra_data = json_data["extra_data"]
|
||||||
|
|
||||||
|
if "client_id" in json_data:
|
||||||
|
extra_data["client_id"] = json_data["client_id"]
|
||||||
|
if valid[0]:
|
||||||
|
prompt_id = str(uuid.uuid4())
|
||||||
|
future = asyncio.get_running_loop().create_future()
|
||||||
|
outputs_to_execute = valid[2]
|
||||||
|
self.prompt_queue.put((number, prompt_id, prompt, extra_data, outputs_to_execute, future))
|
||||||
|
sync_result = await future
|
||||||
|
result = {}
|
||||||
|
for _, (node_id, v) in enumerate(sync_result.items()):
|
||||||
|
for i, item in enumerate(v):
|
||||||
|
result = merge_dict_recursive(result, copy.deepcopy(item))
|
||||||
|
out_string = json.dumps(result)
|
||||||
|
return web.json_response(result, status=resp_code)
|
||||||
|
else:
|
||||||
|
return web.json_response({"error": "no prompt", "node_errors": []}, status=400)
|
||||||
|
|
||||||
|
|
||||||
@routes.post("/queue")
|
@routes.post("/queue")
|
||||||
async def post_queue(request):
|
async def post_queue(request):
|
||||||
json_data = await request.json()
|
json_data = await request.json()
|
||||||
@ -549,6 +608,8 @@ class PromptServer():
|
|||||||
|
|
||||||
if address == '':
|
if address == '':
|
||||||
address = '0.0.0.0'
|
address = '0.0.0.0'
|
||||||
|
self.address = address
|
||||||
|
self.port = port
|
||||||
if verbose:
|
if verbose:
|
||||||
print("Starting server\n")
|
print("Starting server\n")
|
||||||
print("To see the GUI go to: http://{}:{}".format(address, port))
|
print("To see the GUI go to: http://{}:{}".format(address, port))
|
||||||
|
|||||||
@ -228,7 +228,7 @@ app.registerExtension({
|
|||||||
const linkInfo = app.graph.links[l];
|
const linkInfo = app.graph.links[l];
|
||||||
const node = this.graph.getNodeById(linkInfo.target_id);
|
const node = this.graph.getNodeById(linkInfo.target_id);
|
||||||
const input = node.inputs[linkInfo.target_slot];
|
const input = node.inputs[linkInfo.target_slot];
|
||||||
const widgetName = input.widget.name;
|
const widgetName = input.widget?.name;
|
||||||
if (widgetName) {
|
if (widgetName) {
|
||||||
const widget = node.widgets.find((w) => w.name === widgetName);
|
const widget = node.widgets.find((w) => w.name === widgetName);
|
||||||
if (widget) {
|
if (widget) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user