|
@@ -29,6 +29,7 @@ from typing import (
|
|
|
|
|
|
import plotly.graph_objects as go
|
|
|
from plotly.io import to_json
|
|
|
+from redis import Redis
|
|
|
from rich.console import Console
|
|
|
|
|
|
from pynecone import constants
|
|
@@ -385,6 +386,20 @@ def run_frontend_prod(app) -> subprocess.Popen:
|
|
|
return subprocess.Popen(command, cwd=constants.WEB_DIR)
|
|
|
|
|
|
|
|
|
+def get_num_workers() -> int:
|
|
|
+ """Get the number of backend worker processes.
|
|
|
+
|
|
|
+ Returns:
|
|
|
+ The number of backend worker processes.
|
|
|
+ """
|
|
|
+ if get_redis() is None:
|
|
|
+ # If there is no redis, then just use 1 worker.
|
|
|
+ return 1
|
|
|
+
|
|
|
+ # Use the number of cores * 2 + 1.
|
|
|
+ return (os.cpu_count() or 1) * 2 + 1
|
|
|
+
|
|
|
+
|
|
|
def run_backend(app):
|
|
|
"""Run the backend.
|
|
|
|
|
@@ -403,7 +418,14 @@ def run_backend_prod(app) -> None:
|
|
|
Args:
|
|
|
app: The app.
|
|
|
"""
|
|
|
- command = constants.RUN_BACKEND_PROD + [f"{app.__name__}:{constants.API_VAR}"]
|
|
|
+ num_workers = get_num_workers()
|
|
|
+ command = constants.RUN_BACKEND_PROD + [
|
|
|
+ "--workers",
|
|
|
+ str(num_workers),
|
|
|
+ "--threads",
|
|
|
+ str(num_workers),
|
|
|
+ f"{app.__name__}:{constants.APP_VAR}()",
|
|
|
+ ]
|
|
|
subprocess.call(command)
|
|
|
|
|
|
|
|
@@ -918,20 +940,15 @@ def get_hydrate_event(state) -> str:
|
|
|
return get_event(state, constants.HYDRATE)
|
|
|
|
|
|
|
|
|
-def get_redis():
|
|
|
+def get_redis() -> Optional[Redis]:
|
|
|
"""Get the redis client.
|
|
|
|
|
|
Returns:
|
|
|
The redis client.
|
|
|
"""
|
|
|
- try:
|
|
|
- import redis # type: ignore
|
|
|
- except:
|
|
|
- return None
|
|
|
-
|
|
|
config = get_config()
|
|
|
if config.redis_url is None:
|
|
|
return None
|
|
|
redis_url, redis_port = config.redis_url.split(":")
|
|
|
print("Using redis at", config.redis_url)
|
|
|
- return redis.Redis(host=redis_url, port=int(redis_port), db=0)
|
|
|
+ return Redis(host=redis_url, port=int(redis_port), db=0)
|