Skip to content

Commit 7dd6100

Browse files
reload embedded client
1 parent 9f483e8 commit 7dd6100

File tree

2 files changed

+111
-6
lines changed

2 files changed

+111
-6
lines changed

server/api/api.py

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
from pathlib import Path
2-
import os
3-
import json
4-
from git import Repo
5-
import logging
6-
import sys
71
from aiohttp import web
82

93
from api.nodes.nodes import list_nodes, install_node, delete_node
104
from api.models.models import list_models, add_model, delete_model
115
from api.settings.settings import set_twilio_account_info
126

7+
from comfy.nodes.package_typing import ExportedNodes
8+
from comfy.nodes.package import _comfy_nodes, import_all_nodes_in_workspace
9+
from comfy.cmd.execution import nodes
10+
11+
from api.nodes.nodes import force_import_all_nodes_in_workspace
12+
#use a different node import
13+
import_all_nodes_in_workspace = force_import_all_nodes_in_workspace
14+
1315
def add_routes(app):
1416
app.router.add_get("/env/list_nodes", nodes)
1517
app.router.add_post("/env/install_nodes", install_nodes)
@@ -19,9 +21,38 @@ def add_routes(app):
1921
app.router.add_post("/env/add_models", add_models)
2022
app.router.add_post("/env/delete_models", delete_models)
2123

24+
app.router.add_post("/env/reload", reload)
2225
app.router.add_post("/env/set_account_info", set_account_info)
2326

27+
async def reload(request):
28+
'''
29+
Reload ComfyUI environment
2430
31+
'''
32+
33+
#reset embedded client
34+
from comfy.client.embedded_comfy_client import EmbeddedComfyClient
35+
from comfy.cli_args_types import Configuration
36+
await request.app["pipeline"].client.comfy_client.__aexit__()
37+
38+
#reset imports to clear imported nodes
39+
global _comfy_nodes
40+
if len(_comfy_nodes) > 0:
41+
import sys
42+
import importlib
43+
del sys.modules['comfy.nodes.package']
44+
del sys.modules['comfy.cmd.execution']
45+
globals()['comfy.nodes.package'] = importlib.import_module('comfy.nodes.package')
46+
globals()['comfy.cmd.execution'] = importlib.import_module('comfy.cmd.execution')
47+
#use a different node import
48+
import_all_nodes_in_workspace = force_import_all_nodes_in_workspace
49+
_comfy_nodes = import_all_nodes_in_workspace()
50+
51+
#load new embedded client
52+
request.app["pipeline"].client.comfy_client = EmbeddedComfyClient(Configuration(cwd=request.app["workspace"], disable_cuda_malloc=True, gpu_only=True))
53+
54+
return web.json_response({"success": True, "error": None})
55+
2556
async def nodes(request):
2657
'''
2758
List all custom nodes in the workspace
@@ -87,6 +118,24 @@ async def install_nodes(request):
87118
for node in nodes:
88119
await install_node(node, workspace_dir)
89120
installed_nodes.append(node['url'])
121+
122+
#restart embedded client
123+
#request.app["pipeline"].client.set_prompt(request.app["pipeline"].client.prompt)
124+
125+
# reimport nodes to workspace
126+
#from comfy.cmd.execution import nodes
127+
#from comfy.nodes.package import import_all_nodes_in_workspace
128+
#update_nodes = import_all_nodes_in_workspace()
129+
#nodes.update(update_nodes)
130+
131+
#config = request.app["pipeline"].client.comfy_client._configuration
132+
#from comfy.client.embedded_comfy_client import EmbeddedComfyClient
133+
#request.app["pipeline"].client.comfy_client = EmbeddedComfyClient(config)
134+
#await request.app["pipeline"].warm()
135+
136+
#from pipeline import Pipeline
137+
#request.app["pipeline"] = Pipeline(cwd=request.app["workspace"], disable_cuda_malloc=True, gpu_only=True)
138+
90139
return web.json_response({"success": True, "error": None, "installed_nodes": installed_nodes})
91140
except Exception as e:
92141
return web.json_response({"success": False, "error": str(e), "installed_nodes": installed_nodes}, status=500)

server/api/nodes/nodes.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,59 @@ async def delete_node(node, workspace_dir):
127127
except Exception as e:
128128
logger.error(f"error deleting node {node['name']}")
129129
raise Exception(f"error deleting node: {e}")
130+
131+
132+
from comfy.nodes.package import ExportedNodes
133+
from comfy.nodes.package import _comfy_nodes, _import_and_enumerate_nodes_in_module
134+
from functools import reduce
135+
from importlib.metadata import entry_points
136+
import types
137+
138+
def force_import_all_nodes_in_workspace(vanilla_custom_nodes=True, raise_on_failure=False) -> ExportedNodes:
139+
# now actually import the nodes, to improve control of node loading order
140+
from comfy_extras import nodes as comfy_extras_nodes # pylint: disable=absolute-import-used
141+
from comfy.cli_args import args
142+
from comfy.nodes import base_nodes
143+
from comfy.nodes.vanilla_node_importing import mitigated_import_of_vanilla_custom_nodes
144+
145+
# only load these nodes once
146+
147+
base_and_extra = reduce(lambda x, y: x.update(y),
148+
map(lambda module_inner: _import_and_enumerate_nodes_in_module(module_inner, raise_on_failure=raise_on_failure), [
149+
# this is the list of default nodes to import
150+
base_nodes,
151+
comfy_extras_nodes
152+
]),
153+
ExportedNodes())
154+
custom_nodes_mappings = ExportedNodes()
155+
156+
if args.disable_all_custom_nodes:
157+
logging.info("Loading custom nodes was disabled, only base and extra nodes were loaded")
158+
_comfy_nodes.update(base_and_extra)
159+
return _comfy_nodes
160+
161+
# load from entrypoints
162+
for entry_point in entry_points().select(group='comfyui.custom_nodes'):
163+
# Load the module associated with the current entry point
164+
try:
165+
module = entry_point.load()
166+
except ModuleNotFoundError as module_not_found_error:
167+
logging.error(f"A module was not found while importing nodes via an entry point: {entry_point}. Please ensure the entry point in setup.py is named correctly", exc_info=module_not_found_error)
168+
continue
169+
170+
# Ensure that what we've loaded is indeed a module
171+
if isinstance(module, types.ModuleType):
172+
custom_nodes_mappings.update(
173+
_import_and_enumerate_nodes_in_module(module, print_import_times=True))
174+
175+
# load the vanilla custom nodes last
176+
if vanilla_custom_nodes:
177+
custom_nodes_mappings += mitigated_import_of_vanilla_custom_nodes()
178+
179+
# don't allow custom nodes to overwrite base nodes
180+
custom_nodes_mappings -= base_and_extra
181+
182+
_comfy_nodes.update(base_and_extra + custom_nodes_mappings)
183+
184+
return _comfy_nodes
185+

0 commit comments

Comments
 (0)