-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
85 lines (69 loc) · 2.23 KB
/
main.py
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
import server
import asyncio
import webbrowser
import execution
import execution_queue
import threading
import internal.utils
from nodes import init_custom_nodes
import time
import gc
from server import BinaryEventTypes, PyGraphServer
def runner_worker(queue: execution_queue.RunnerQueue, server):
e = execution.RunnerExecutor(server)
while True:
item, item_id = queue.get()
execution_start_time = time.perf_counter()
runner_id = item[1]
e.execute(item[2], runner_id, item[3], item[4])
queue.task_done(item_id, e.outputs_ui)
if server.client_id is not None:
server.send_sync(
"executing", {"node": None,
"runner_id": runner_id}, server.client_id
)
print(
"runner executed in {:.2f} seconds".format(
time.perf_counter() - execution_start_time
)
)
# 手动触发垃圾回收。
gc.collect()
async def run(
server: PyGraphServer, address="", port=8188, verbose=True, call_on_start=None
):
await asyncio.gather(
server.start(address, port, verbose,
call_on_start), server.publish_loop()
)
def hijack_progress(server):
def hook(value, total, preview: internal.utils.PreviewType):
server.send_sync(
"progress", {"value": value, "max": total}, server.client_id)
if preview is not None:
if preview.type == "image":
server.send_sync(
BinaryEventTypes.UNENCODED_PREVIEW_IMAGE,
preview.data,
server.client_id,
)
internal.utils.set_progress_bar_global_hook(hook)
if __name__ == "__main__":
init_custom_nodes()
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
server = server.PyGraphServer(loop)
queue = execution_queue.RunnerQueue(server)
server.add_routes()
hijack_progress(server)
webbrowser.open("http://127.0.0.1:5000")
threading.Thread(
target=runner_worker,
daemon=True,
args=(
queue,
server,
),
).start()
loop.run_until_complete(run(server, "127.0.0.1", 5000))
loop.run_forever()