-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprestartup_script.py
More file actions
104 lines (83 loc) · 3.59 KB
/
Copy pathprestartup_script.py
File metadata and controls
104 lines (83 loc) · 3.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
"""
node_load_markers - prestartup_script.py
Wraps ComfyUI's custom node loader (nodes.load_custom_node) so the
console clearly shows which node is currently loading: a START/END
marker around each node, and a [node_name] tag on every line printed
while that node loads.
The END marker for a node is NOT printed immediately - it's held back
until the NEXT node's START is about to print (or the process exits).
This way, if a node writes something from a background thread shortly
after its own import call returns, that output still tends to land
visually inside the correct block instead of appearing after the
next node's START.
"""
import sys
import time
import atexit
class _TaggingStream:
"""Prefixes every line with a [node_name] tag."""
def __init__(self, original, tag):
self._original = original
self._tag = tag
self._at_line_start = True
def write(self, s):
if not s:
return
out = []
for ch in s:
if self._at_line_start and ch != "\n":
out.append(f"[{self._tag}] ")
self._at_line_start = False
out.append(ch)
if ch == "\n":
self._at_line_start = True
self._original.write("".join(out))
def flush(self):
self._original.flush()
def __getattr__(self, item):
return getattr(self._original, item)
try:
import nodes
if not getattr(nodes, "_node_load_markers_patched", False):
_original_load_custom_node = nodes.load_custom_node
_pending_end = {"line": None}
def _flush_pending_end():
if _pending_end["line"] is not None:
print(_pending_end["line"])
sys.stdout.flush()
_pending_end["line"] = None
atexit.register(_flush_pending_end)
def _patched_load_custom_node(module_path, *args, **kwargs):
name = getattr(module_path, "name", str(module_path))
tag = name.rstrip("\\/").split("\\")[-1].split("/")[-1]
# flush the previous node's pending END line before this
# node starts writing to the console
_flush_pending_end()
print(f"\n===== START: {name} =====")
sys.stdout.flush()
sys.stderr.flush()
old_stdout, old_stderr = sys.stdout, sys.stderr
sys.stdout = _TaggingStream(old_stdout, tag)
sys.stderr = _TaggingStream(old_stderr, tag)
start = time.time()
try:
result = _original_load_custom_node(module_path, *args, **kwargs)
elapsed = time.time() - start
status = "OK" if result is not False else "FAILED (returned False)"
sys.stdout, sys.stderr = old_stdout, old_stderr
_pending_end["line"] = f"===== END: {name} [{status}, {elapsed:.2f}s] =====\n"
return result
except Exception as e:
elapsed = time.time() - start
sys.stdout, sys.stderr = old_stdout, old_stderr
_pending_end["line"] = f"===== END: {name} [EXCEPTION: {e}, {elapsed:.2f}s] =====\n"
raise
finally:
sys.stdout, sys.stderr = old_stdout, old_stderr
nodes.load_custom_node = _patched_load_custom_node
nodes._node_load_markers_patched = True
print("[node_load_markers] load_custom_node patched - line tagging + deferred end markers active")
else:
print("[node_load_markers] already patched, skipping")
except Exception as e:
print(f"[node_load_markers] could not patch nodes.load_custom_node: {e}")