i0switch commited on
Commit
4b8265b
·
verified ·
1 Parent(s): fad8f4c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -10
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
- port_env = int(os.getenv("PORT", "7860"))
222
- try:
223
- uvicorn.run(app, host="0.0.0.0", port=port_env, workers=1, log_level="info")
224
- except OSError as e:
225
- if e.errno == 98 and port_env != 7860:
226
- print(f"⚠️ Port {port_env} busy → falling back to 7860")
227
- uvicorn.run(app, host="0.0.0.0", port=7860, workers=1, log_level="info")
228
- else:
229
- raise
 
 
 
 
 
 
 
 
 
 
 
 
 
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"