Skip to content

Commit d390d89

Browse files
authored
reattempt at granian as default (#5165)
* reattempt at granian as default * use filename instead of module * current_work_directory
1 parent d7f1cf5 commit d390d89

File tree

3 files changed

+63
-39
lines changed

3 files changed

+63
-39
lines changed

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ dependencies = [
2323
"alembic >=1.15.2,<2.0",
2424
"fastapi >=0.115.0",
2525
"granian[reload] >=2.2.5",
26-
"gunicorn >=23.0.0,<24.0.0",
2726
"httpx >=0.28.0,<1.0",
2827
"jinja2 >=3.1.2,<4.0",
2928
"packaging >=24.2,<26",
@@ -38,7 +37,6 @@ dependencies = [
3837
"sqlmodel >=0.0.24,<0.1",
3938
"typer >=0.15.2,<1.0",
4039
"typing_extensions >=4.13.0",
41-
"uvicorn >=0.34.0",
4240
"wrapt >=1.17.0,<2.0",
4341
]
4442
classifiers = [
@@ -166,4 +164,6 @@ dev = [
166164
"ruff >=0.11",
167165
"selenium >=4.31",
168166
"starlette-admin >=0.14",
167+
"uvicorn >=0.34.0",
168+
169169
]

reflex/utils/exec.py

Lines changed: 59 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,9 @@ def run_frontend_prod(root: Path, port: str, backend_present: bool = True):
189189

190190
@once
191191
def _warn_user_about_uvicorn():
192-
# When we eventually switch to Granian by default, we should enable this warning.
193-
if False:
194-
console.warn(
195-
"Using Uvicorn for backend as it is installed. This behavior will change in 0.8.0 to use Granian by default."
196-
)
192+
console.warn(
193+
"Using Uvicorn for backend as it is installed. This behavior will change in 0.8.0 to use Granian by default."
194+
)
197195

198196

199197
def should_use_granian():
@@ -202,8 +200,8 @@ def should_use_granian():
202200
Returns:
203201
True if Granian should be used.
204202
"""
205-
if environment.REFLEX_USE_GRANIAN.get():
206-
return True
203+
if environment.REFLEX_USE_GRANIAN.is_set():
204+
return environment.REFLEX_USE_GRANIAN.get()
207205
if (
208206
importlib.util.find_spec("uvicorn") is None
209207
or importlib.util.find_spec("gunicorn") is None
@@ -219,9 +217,51 @@ def get_app_module():
219217
Returns:
220218
The app module for the backend.
221219
"""
222-
config = get_config()
220+
return get_config().module
221+
222+
223+
def get_app_instance():
224+
"""Get the app module for the backend.
225+
226+
Returns:
227+
The app module for the backend.
228+
"""
229+
return f"{get_app_module()}:{constants.CompileVars.APP}"
223230

224-
return f"{config.module}:{constants.CompileVars.APP}"
231+
232+
def get_app_file() -> Path:
233+
"""Get the app file for the backend.
234+
235+
Returns:
236+
The app file for the backend.
237+
238+
Raises:
239+
ImportError: If the app module is not found.
240+
"""
241+
current_working_dir = str(Path.cwd())
242+
if current_working_dir not in sys.path:
243+
# Add the current working directory to sys.path
244+
sys.path.insert(0, current_working_dir)
245+
module_spec = importlib.util.find_spec(get_app_module())
246+
if module_spec is None:
247+
raise ImportError(
248+
f"Module {get_app_module()} not found. Make sure the module is installed."
249+
)
250+
file_name = module_spec.origin
251+
if file_name is None:
252+
raise ImportError(
253+
f"Module {get_app_module()} not found. Make sure the module is installed."
254+
)
255+
return Path(file_name).resolve()
256+
257+
258+
def get_app_instance_from_file() -> str:
259+
"""Get the app module for the backend.
260+
261+
Returns:
262+
The app module for the backend.
263+
"""
264+
return f"{get_app_file()}:{constants.CompileVars.APP}"
225265

226266

227267
def run_backend(
@@ -323,7 +363,7 @@ def run_uvicorn_backend(host: str, port: int, loglevel: LogLevel):
323363
import uvicorn
324364

325365
uvicorn.run(
326-
app=f"{get_app_module()}",
366+
app=f"{get_app_instance()}",
327367
factory=True,
328368
host=host,
329369
port=port,
@@ -349,7 +389,7 @@ def run_granian_backend(host: str, port: int, loglevel: LogLevel):
349389
from granian.server import MPServer as Granian
350390

351391
Granian(
352-
target=get_app_module(),
392+
target=get_app_instance_from_file(),
353393
factory=True,
354394
address=host,
355395
port=port,
@@ -367,14 +407,12 @@ def _deprecate_asgi_config(
367407
config_name: str,
368408
reason: str = "",
369409
):
370-
# When we eventually switch to Granian by default, we should enable this deprecation.
371-
if False:
372-
console.deprecate(
373-
f"config.{config_name}",
374-
reason=reason,
375-
deprecation_version="0.7.5",
376-
removal_version="0.8.0",
377-
)
410+
console.deprecate(
411+
f"config.{config_name}",
412+
reason=reason,
413+
deprecation_version="0.7.9",
414+
removal_version="0.8.0",
415+
)
378416

379417

380418
@once
@@ -468,7 +506,7 @@ def run_uvicorn_backend_prod(host: str, port: int, loglevel: LogLevel):
468506

469507
config = get_config()
470508

471-
app_module = get_app_module()
509+
app_module = get_app_instance()
472510

473511
command = (
474512
[
@@ -568,7 +606,7 @@ def run_granian_backend_prod(host: str, port: int, loglevel: LogLevel):
568606
*("--host", host),
569607
*("--port", str(port)),
570608
*("--interface", str(Interfaces.ASGI)),
571-
*("--factory", get_app_module()),
609+
*("--factory", get_app_instance_from_file()),
572610
]
573611
processes.new_process(
574612
command,

uv.lock

Lines changed: 2 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)