Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -217,13 +217,25 @@ app = gr.mount_gradio_app(app, demo, path="/")
|
|
217 |
|
218 |
print("Application startup script finished. Waiting for requests.")
|
219 |
if __name__ == "__main__":
|
220 |
-
import uvicorn
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
217 |
|
218 |
print("Application startup script finished. Waiting for requests.")
|
219 |
if __name__ == "__main__":
|
220 |
+
import os, time, socket, uvicorn
|
221 |
+
|
222 |
+
def port_is_free(port: int) -> bool:
|
223 |
+
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
224 |
+
return s.connect_ex(("0.0.0.0", port)) != 0
|
225 |
+
|
226 |
+
port = int(os.getenv("PORT", 7860))
|
227 |
+
timeout_sec = 30 # 30 秒だけ待つ
|
228 |
+
poll_interval = 2 # 2 秒ごとに再チェック
|
229 |
+
|
230 |
+
t0 = time.time()
|
231 |
+
while not port_is_free(port):
|
232 |
+
waited = time.time() - t0
|
233 |
+
if waited >= timeout_sec:
|
234 |
+
raise RuntimeError(f"Port {port} is still busy after {timeout_sec}s")
|
235 |
+
print(f"⚠️ Port {port} busy, retrying in {poll_interval}s …")
|
236 |
+
time.sleep(poll_interval)
|
237 |
+
|
238 |
+
# 空いたら起動
|
239 |
+
uvicorn.run(
|
240 |
+
app, host="0.0.0.0", port=port,
|
241 |
+
workers=1, log_level="info"
|