Workaround for Gradio server hanging on keyboard interrupt

Moved the demo.launch() call to start up a new thread, so we can kill the process ourselves on keyboard interrupt
This commit is contained in:
EyeDeck 2022-08-25 08:08:24 -04:00
parent a6f80800f3
commit 5e9ed5961d

View File

@ -6,7 +6,7 @@ import mimetypes
import numpy as np
import pynvml
import random
import threading
import threading, asyncio
import time
import torch
import torch.nn as nn
@ -1010,5 +1010,28 @@ demo = gr.TabbedInterface(
css=("" if opt.no_progressbar_hiding else css_hide_progressbar),
theme="default",
)
demo.queue(concurrency_count=1)
demo.launch(show_error=True, server_name='0.0.0.0')
class ServerLauncher(threading.Thread):
def __init__(self, demo):
threading.Thread.__init__(self)
self.name = 'Gradio Server Thread'
self.demo = demo
def run(self):
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
demo.launch(show_error=True, server_name='0.0.0.0')
def stop(self):
demo.close() # this tends to hang
server_thread = ServerLauncher(demo)
server_thread.start()
try:
while server_thread.is_alive():
time.sleep(60)
except (KeyboardInterrupt, OSError) as e:
crash(e, 'Shutting down...')