Browse Source

dev-mode compile: purge .web dir at last min to reduce downtime window (#1430)

jackie-pc 1 year ago
parent
commit
7a09554cfa
4 changed files with 12 additions and 6 deletions
  1. 2 1
      .gitignore
  2. 1 0
      .pre-commit-config.yaml
  3. 8 4
      reflex/app.py
  4. 1 1
      reflex/utils/imports.py

+ 2 - 1
.gitignore

@@ -4,4 +4,5 @@ dist/*
 examples/
 .idea
 .vscode
-.coverage
+.coverage
+venv

+ 1 - 0
.pre-commit-config.yaml

@@ -3,6 +3,7 @@ repos:
     rev: v0.0.244
     hooks:
     - id: ruff
+      args: [--fix, --exit-non-zero-on-fix]
 
   - repo: https://github.com/RobertCraigie/pyright-python
     rev: v1.1.313

+ 8 - 4
reflex/app.py

@@ -467,23 +467,23 @@ class App(Base):
         )
         task = progress.add_task("Compiling: ", total=len(self.pages))
 
+        # TODO: include all work done in progress indicator, not just self.pages
         for render, kwargs in DECORATED_ROUTES:
             self.add_page(render, **kwargs)
 
         # Get the env mode.
         config = get_config()
 
-        # Empty the .web pages directory
-        compiler.purge_web_pages_dir()
-
         # Store the compile results.
         compile_results = []
 
         # Compile the pages in parallel.
         custom_components = set()
+        # TODO Anecdotally, processes=2 works 10% faster (cpu_count=12)
         thread_pool = ThreadPool()
         with progress:
             for route, component in self.pages.items():
+                # TODO: this progress does not reflect actual threaded task completion
                 progress.advance(task)
                 component.add_style(self.style)
                 compile_results.append(
@@ -505,6 +505,8 @@ class App(Base):
         # Get the results.
         compile_results = [result.get() for result in compile_results]
 
+        # TODO the compile tasks below may also benefit from parallelization too
+
         # Compile the custom components.
         compile_results.append(compiler.compile_components(custom_components))
 
@@ -521,10 +523,12 @@ class App(Base):
             )
             compile_results.append(compiler.compile_tailwind(config.tailwind))
 
+        # Empty the .web pages directory
+        compiler.purge_web_pages_dir()
+
         # Write the pages at the end to trigger the NextJS hot reload only once.
         thread_pool = ThreadPool()
         for output_path, code in compile_results:
-            compiler_utils.write_page(output_path, code)
             thread_pool.apply_async(compiler_utils.write_page, args=(output_path, code))
         thread_pool.close()
         thread_pool.join()

+ 1 - 1
reflex/utils/imports.py

@@ -9,7 +9,7 @@ ImportDict = Dict[str, Set[ImportVar]]
 
 
 def merge_imports(*imports) -> ImportDict:
-    """Merge two import dicts together.
+    """Merge multiple import dicts together.
 
     Args:
         *imports: The list of import dicts to merge.