File size: 4,825 Bytes
d248b55 |
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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import socket\n",
"import time\n",
"import requests\n",
"import subprocess\n",
"import os\n",
"\n",
"class ComfyUI:\n",
" def __init__(self):\n",
" self.process = None\n",
" self.port = None\n",
" self.SS()\n",
"\n",
" def is_port_in_use(self , port , host='127.0.0.1'):\n",
" with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:\n",
" s.settimeout(1)\n",
" try:\n",
" s.bind((host, port))\n",
" except socket.error:\n",
" return True \n",
" return False \n",
" \n",
" def is_server_ready(self , url, timeout=30):\n",
" start_time = time.time()\n",
" while True:\n",
" try:\n",
" response = requests.get(url)\n",
" if response.status_code == 200:\n",
" return True\n",
" except requests.RequestException:\n",
" pass\n",
" \n",
" if time.time() - start_time > timeout:\n",
" return False\n",
" time.sleep(1) \n",
" \n",
" def SS(self):\n",
" try:\n",
" port_to_check = 60000\n",
" con_count = 0\n",
" while(True):\n",
" if con_count > 100:\n",
" return False\n",
" if self.is_port_in_use(port_to_check):\n",
" port_to_check = port_to_check + 1\n",
" con_count = con_count + 1\n",
" else:\n",
" break\n",
" \n",
" port_to_check = str(port_to_check)\n",
" script_dir = \"./ComfyUI/ComfyUI\"\n",
" script_dir = os.path.abspath(script_dir)\n",
"\n",
" code_dir = \"./ComfyUI/ComfyUI/main.py\"\n",
" code_dir = os.path.abspath(code_dir)\n",
"\n",
" command = [\"/home/itek/miniconda3/envs/ai/bin/python\", os.path.abspath(code_dir), \"--port\", port_to_check, '--listen' , '127.0.0.1']\n",
" self.process = subprocess.Popen(command, cwd=script_dir , stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)\n",
" self.port = port_to_check\n",
"\n",
" return True\n",
" \n",
" except Exception as e: \n",
" print(e)\n",
" return False\n",
" \n",
" def restart_if_down(self):\n",
"\n",
" if self.port is None:\n",
" return False\n",
" \n",
" server_url = f\"http://127.0.0.1:{self.port}\"\n",
" if not self.is_server_ready(server_url, timeout=5):\n",
" \n",
" if self.process is not None:\n",
" self.process.terminate()\n",
" self.process.kill()\n",
" self.process = None\n",
" self.port = None\n",
"\n",
" return self.SS()\n",
"\n",
" return True\n",
"\n",
" def __del__(self):\n",
" if self.process is not None:\n",
" self.process.terminate()\n",
" self.process.kill()"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"ui = ComfyUI()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ui.restart_if_down()"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"ui.process.kill()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'60001'"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ui.port"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "ai",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|