Merge pull request #3 from drip-art/yo-add-ci-flag

Add flag for supporting running comfy as a script
This commit is contained in:
Yoland Yan 2024-04-04 16:46:00 -07:00 committed by GitHub
commit 3e84b665c0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 66 additions and 5 deletions

View File

@ -1,5 +1,7 @@
import argparse import argparse
import base64
import enum import enum
import json
import comfy.options import comfy.options
class EnumAction(argparse.Action): class EnumAction(argparse.Action):
@ -107,8 +109,12 @@ parser.add_argument("--disable-smart-memory", action="store_true", help="Force C
parser.add_argument("--deterministic", action="store_true", help="Make pytorch use slower deterministic algorithms when it can. Note that this might not make images deterministic in all cases.") parser.add_argument("--deterministic", action="store_true", help="Make pytorch use slower deterministic algorithms when it can. Note that this might not make images deterministic in all cases.")
parser.add_argument("--dont-print-server", action="store_true", help="Don't print server output.") parser.add_argument("--dont-print-server", action="store_true", help="Don't print server output.")
parser.add_argument("--quick-test-for-ci", action="store_true", help="Quick test for CI.")
parser.add_argument("--windows-standalone-build", action="store_true", help="Windows standalone build: Enable convenient things that most people using the standalone windows build will probably enjoy (like auto opening the page on startup).") parser.add_argument("--windows-standalone-build", action="store_true", help="Windows standalone build: Enable convenient things that most people using the standalone windows build will probably enjoy (like auto opening the page on startup).")
ci_group = parser.add_mutually_exclusive_group()
ci_group.add_argument("--quick-test-for-ci", action="store_true", help="Quick test for CI.")
ci_group.add_argument("--test-ci-with-prompts", action="store_true", help="Test CI with prompts and Comfy will stop running after prompts are complete.")
parser.add_argument("--prompts-for-ci-test", nargs="+", default=[], type=lambda x: json.loads(base64.b64decode(x).decode("utf-8")), help="List of base64 encoded JSON strings for testing CI with prompts. Prompts to use for CI test when --test-ci-with-prompts is set.")
parser.add_argument("--disable-metadata", action="store_true", help="Disable saving prompt metadata in files.") parser.add_argument("--disable-metadata", action="store_true", help="Disable saving prompt metadata in files.")

View File

@ -721,6 +721,7 @@ class PromptQueue:
self.history = {} self.history = {}
self.flags = {} self.flags = {}
server.prompt_queue = self server.prompt_queue = self
self.stop_server_upon_completion = False
def put(self, item): def put(self, item):
with self.mutex: with self.mutex:
@ -763,6 +764,8 @@ class PromptQueue:
'status': status_dict, 'status': status_dict,
} }
self.server.queue_updated() self.server.queue_updated()
if self.stop_server_upon_completion and len(self.queue) == 0 and len(self.currently_running) == 0:
self.server.stop()
def get_current_queue(self): def get_current_queue(self):
with self.mutex: with self.mutex:
@ -819,6 +822,10 @@ class PromptQueue:
def delete_history_item(self, id_to_delete): def delete_history_item(self, id_to_delete):
with self.mutex: with self.mutex:
self.history.pop(id_to_delete, None) self.history.pop(id_to_delete, None)
def set_stop_server_upon_completion(self, value):
with self.mutex:
self.stop_server_upon_completion = value
def set_flag(self, name, data): def set_flag(self, name, data):
with self.mutex: with self.mutex:

View File

@ -1,3 +1,4 @@
import uuid
import comfy.options import comfy.options
comfy.options.enable_args_parsing() comfy.options.enable_args_parsing()
@ -241,6 +242,14 @@ if __name__ == "__main__":
if args.quick_test_for_ci: if args.quick_test_for_ci:
exit(0) exit(0)
if args.test_ci_with_prompts:
q.set_stop_server_upon_completion(True)
for prompt_json in args.prompts_for_ci_test:
prompt_id = str(uuid.uuid4())
validation = execution.validate_prompt(prompt_json)
outputs_to_execute = validation[2]
q.put((0, prompt_id, prompt_json, {}, outputs_to_execute))
call_on_start = None call_on_start = None
if args.auto_launch: if args.auto_launch:
def startup_server(address, port): def startup_server(address, port):

View File

@ -73,6 +73,8 @@ class PromptServer():
self.loop = loop self.loop = loop
self.messages = asyncio.Queue() self.messages = asyncio.Queue()
self.number = 0 self.number = 0
self.runner = None
self.site = None
middlewares = [cache_control] middlewares = [cache_control]
if args.enable_cors_header: if args.enable_cors_header:
@ -475,6 +477,7 @@ class PromptServer():
if valid[0]: if valid[0]:
prompt_id = str(uuid.uuid4()) prompt_id = str(uuid.uuid4())
outputs_to_execute = valid[2] outputs_to_execute = valid[2]
# TODO: refactor PromptQueue to take typed input
self.prompt_queue.put((number, prompt_id, prompt, extra_data, outputs_to_execute)) self.prompt_queue.put((number, prompt_id, prompt, extra_data, outputs_to_execute))
response = {"prompt_id": prompt_id, "number": number, "node_errors": valid[3]} response = {"prompt_id": prompt_id, "number": number, "node_errors": valid[3]}
return web.json_response(response) return web.json_response(response)
@ -618,13 +621,16 @@ class PromptServer():
async def publish_loop(self): async def publish_loop(self):
while True: while True:
msg = await self.messages.get() msg = await self.messages.get()
# Break the message loop with None message
if msg is None:
break
await self.send(*msg) await self.send(*msg)
async def start(self, address, port, verbose=True, call_on_start=None): async def start(self, address, port, verbose=True, call_on_start=None):
runner = web.AppRunner(self.app, access_log=None) self.runner = web.AppRunner(self.app, access_log=None)
await runner.setup() await self.runner.setup()
site = web.TCPSite(runner, address, port) self.site = web.TCPSite(self.runner, address, port)
await site.start() await self.site.start()
if verbose: if verbose:
logging.info("Starting server\n") logging.info("Starting server\n")
@ -632,6 +638,39 @@ class PromptServer():
if call_on_start is not None: if call_on_start is not None:
call_on_start(address, port) call_on_start(address, port)
# TODO: accept stop API request
def stop(self):
"""
Stop the server.
Call `async_stop` coroutine in a thread-safe manner
If the event loop is not running, the stop() is called too early
"""
logging.info("Stopping server")
if self.loop.is_running():
asyncio.run_coroutine_threadsafe(self.async_stop(), self.loop)
else:
logging.error("Cannot stop server, loop is not running")
async def async_stop(self):
"""
Asynchronously stops the server.
This coroutine should be called to gracefully stop the server.
"""
# Put breaking message for message queue
await self.messages.put(None)
# TODO: check if queue is not yet complete and conditional shutdown
# server
if not self.site or self.site._closed: # Check if the site is already closed
logging.warning("Server is already stopped or was never started.")
return
if self.site:
await self.site.stop()
if self.runner:
await self.runner.cleanup()
logging.info("Stopped site upon request")
def add_on_prompt_handler(self, handler): def add_on_prompt_handler(self, handler):
self.on_prompt_handlers.append(handler) self.on_prompt_handlers.append(handler)