Skip to content

Commit

Permalink
Do not load code in gr.NO_RELOAD in the reload mode watch thread (#9886)
Browse files Browse the repository at this point in the history
* add code

* add changeset

* no if __name__ reload twice

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
  • Loading branch information
freddyaboulton and gradio-pr-bot authored Nov 4, 2024
1 parent b6725cf commit fa5d433
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/wet-knives-type.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"gradio": patch
---

fix:Do not load code in gr.NO_RELOAD in the reload mode watch thread
2 changes: 2 additions & 0 deletions gradio/http_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import os
import socket
import sys
import threading
import time
from functools import partial
Expand Down Expand Up @@ -144,6 +145,7 @@ def start_server(
demo_name=GRADIO_WATCH_DEMO_NAME,
stop_event=threading.Event(),
demo_file=GRADIO_WATCH_DEMO_PATH,
watch_module=sys.modules["__main__"],
)
server = Server(config=config, reloader=reloader)
server.run_in_thread()
Expand Down
23 changes: 21 additions & 2 deletions gradio/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ def __init__(
watch_dirs: list[str],
watch_module_name: str,
demo_file: str,
watch_module: ModuleType,
stop_event: threading.Event,
demo_name: str = "demo",
) -> None:
Expand All @@ -146,6 +147,7 @@ def __init__(
self.stop_event = stop_event
self.demo_name = demo_name
self.demo_file = Path(demo_file)
self.watch_module = watch_module

@property
def running_app(self) -> App:
Expand Down Expand Up @@ -195,9 +197,22 @@ def _is_gr_no_reload(expr: ast.AST) -> bool:
and expr.test.attr == "NO_RELOAD"
)

def _is_if_name_main(expr: ast.AST) -> bool:
"""Find the if __name__ == '__main__': block."""
return (
isinstance(expr, ast.If)
and isinstance(expr.test, ast.Compare)
and isinstance(expr.test.left, ast.Name)
and expr.test.left.id == "__name__"
and len(expr.test.ops) == 1
and isinstance(expr.test.ops[0], ast.Eq)
and isinstance(expr.test.comparators[0], ast.Constant)
and expr.test.comparators[0].s == "__main__"
)

# Find the positions of the code blocks to load
for node in ast.walk(tree):
if _is_gr_no_reload(node):
if _is_gr_no_reload(node) or _is_if_name_main(node):
assert isinstance(node, ast.If) # noqa: S101
node.body = [ast.Pass(lineno=node.lineno, col_offset=node.col_offset)]

Expand Down Expand Up @@ -259,7 +274,11 @@ def iter_py_files() -> Iterator[Path]:
mtimes = {}
# Need to import the module in this thread so that the
# module is available in the namespace of this thread
module = importlib.import_module(reloader.watch_module_name)
module = reloader.watch_module
no_reload_source_code = _remove_no_reload_codeblocks(str(reloader.demo_file))
exec(no_reload_source_code, module.__dict__)
sys.modules[reloader.watch_module_name] = module

while reloader.should_watch():
changed = get_changes()
if changed:
Expand Down

0 comments on commit fa5d433

Please sign in to comment.