|
8 | 8 | import os
|
9 | 9 | import re
|
10 | 10 | from PIL import Image
|
| 11 | +import time |
| 12 | +import psutil |
| 13 | +import GPUtil |
| 14 | +import platform |
| 15 | +import subprocess |
| 16 | +from comfy.cli_args import args |
11 | 17 |
|
12 | 18 | def get_extension_calling():
|
13 | 19 | for frame in inspect.stack():
|
@@ -454,6 +460,119 @@ def update_absolute(self, value, total=None, preview=None):
|
454 | 460 | self.current = value
|
455 | 461 | if self.hook is not None:
|
456 | 462 | self.hook(self.current, self.total, preview)
|
| 463 | + wait_cooldown(kind="progress") |
457 | 464 |
|
458 | 465 | def update(self, value):
|
459 | 466 | self.update_absolute(self.current + value)
|
| 467 | + |
| 468 | +def clear_line(n=1): |
| 469 | + LINE_UP = '\033[1A' |
| 470 | + LINE_CLEAR = '\x1b[2K' |
| 471 | + for i in range(n): |
| 472 | + print(LINE_UP, end=LINE_CLEAR) |
| 473 | + |
| 474 | +def func_sleep(seconds, pbar=None): |
| 475 | + while seconds > 0: |
| 476 | + print(f"Sleeping {seconds} seconds") |
| 477 | + time.sleep(1) |
| 478 | + seconds -= 1 |
| 479 | + clear_line() |
| 480 | + if pbar is not None: |
| 481 | + pbar.update(1) |
| 482 | + |
| 483 | +def get_processor_name(): |
| 484 | + if platform.system() == "Windows": |
| 485 | + return platform.processor() |
| 486 | + elif platform.system() == "Darwin": |
| 487 | + os.environ['PATH'] = os.environ['PATH'] + os.pathsep + '/usr/sbin' |
| 488 | + command ="sysctl -n machdep.cpu.brand_string" |
| 489 | + return subprocess.check_output(command).strip() |
| 490 | + elif platform.system() == "Linux": |
| 491 | + command = "cat /proc/cpuinfo" |
| 492 | + all_info = subprocess.check_output(command, shell=True).decode().strip() |
| 493 | + for line in all_info.split("\n"): |
| 494 | + if "model name" in line: |
| 495 | + return re.sub(".*model name.*:", "", line, 1).strip() |
| 496 | + return "" |
| 497 | + |
| 498 | +def get_temperatures(): |
| 499 | + temperatures = [] |
| 500 | + |
| 501 | + if platform.system() == "Linux": |
| 502 | + cpu_max_temp = 0 |
| 503 | + |
| 504 | + for k, v in psutil.sensors_temperatures(fahrenheit=False).items(): |
| 505 | + for t in v: |
| 506 | + if t.current > cpu_max_temp: |
| 507 | + cpu_max_temp = t.current |
| 508 | + |
| 509 | + temperatures.append({ |
| 510 | + "label": get_processor_name(), |
| 511 | + "temperature": cpu_max_temp, |
| 512 | + "kind": "CPU", |
| 513 | + }) |
| 514 | + |
| 515 | + for gpu in GPUtil.getGPUs(): |
| 516 | + temperatures.append({ |
| 517 | + "label": gpu.name, |
| 518 | + "temperature": gpu.temperature, |
| 519 | + "kind": "GPU", |
| 520 | + }) |
| 521 | + |
| 522 | + return temperatures |
| 523 | + |
| 524 | +waiting_cooldown = False |
| 525 | + |
| 526 | +def _wait_cooldown(max_temperature=70, safe_temperature=60, seconds=2, max_seconds=0): |
| 527 | + global waiting_cooldown |
| 528 | + |
| 529 | + if waiting_cooldown: |
| 530 | + return |
| 531 | + |
| 532 | + waiting_cooldown = True |
| 533 | + |
| 534 | + try: |
| 535 | + max_temperature, safe_temperature = max(max_temperature, safe_temperature), min(max_temperature, safe_temperature) |
| 536 | + |
| 537 | + if max_temperature <= 0: |
| 538 | + return |
| 539 | + |
| 540 | + if safe_temperature <= 0: |
| 541 | + safe_temperature = max_temperature |
| 542 | + |
| 543 | + if max_seconds == 0: |
| 544 | + max_seconds = 0xffffffffffffffff |
| 545 | + |
| 546 | + seconds = max(1, seconds) |
| 547 | + max_seconds = max(seconds, max_seconds) |
| 548 | + times = max_seconds // seconds |
| 549 | + |
| 550 | + hot = True |
| 551 | + |
| 552 | + # Start with the max temperature, so if not above it don't cool down. |
| 553 | + limit_temperature = max_temperature |
| 554 | + |
| 555 | + while hot and times > 0: |
| 556 | + temperatures = [f"{t['kind']} {t['label']}: {t['temperature']}" for t in get_temperatures() if t["temperature"] > limit_temperature] |
| 557 | + hot = len(temperatures) > 0 |
| 558 | + |
| 559 | + if hot: |
| 560 | + # Switch to safe temperature to cool down to that temperature |
| 561 | + limit_temperature = safe_temperature |
| 562 | + print(f"Too hot! Limit temperature: [ {limit_temperature} ] Current temperature: [ " + " | ".join(temperatures) + " ]") |
| 563 | + pbar = ProgressBar(seconds) |
| 564 | + func_sleep(seconds, pbar) |
| 565 | + clear_line() |
| 566 | + times -= 1 |
| 567 | + finally: |
| 568 | + waiting_cooldown = False |
| 569 | + |
| 570 | +def wait_cooldown(kind="execution"): |
| 571 | + safe_temperature = args.safe_progress_temperature if kind == "progress" else args.safe_temperature |
| 572 | + if safe_temperature > 0: |
| 573 | + _wait_cooldown( |
| 574 | + max_temperature=args.max_temperature, |
| 575 | + safe_temperature=safe_temperature, |
| 576 | + seconds=args.each_cool_down_seconds, |
| 577 | + max_seconds=args.max_cool_down_seconds, |
| 578 | + ) |
0 commit comments