Skip to content

Commit

Permalink
Merge pull request #185 from ivanyu/ivanyu/gh-184-dont-doublestart-flask
Browse files Browse the repository at this point in the history
ui: Don't load heap file twice while Flask is restarting itself
  • Loading branch information
ivanyu authored Nov 20, 2022
2 parents 66809e8 + f081e79 commit 09e00a4
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions pyheap-ui/src/pyheap_ui/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#
import argparse
import dataclasses
import logging
import mmap
import os
import time
Expand Down Expand Up @@ -51,6 +52,10 @@ class MyFlask(Flask):


app = MyFlask(__name__)
app.logger.setLevel(logging.INFO)
if app.logger.handlers:
formatter = logging.Formatter("[%(asctime)s] %(levelname)s %(message)s")
app.logger.handlers[0].setFormatter(formatter)

heap: Optional[Heap] = None
inbound_references: Optional[InboundReferences] = None
Expand Down Expand Up @@ -159,18 +164,21 @@ def big_number(size: int) -> str:
parser.add_argument("--file", "-f", type=str, required=True, help="heap file name")
args = parser.parse_args()

start = time.monotonic()
app.logger.info("Loading file %s", args.file)
with open(args.file, "rb") as f:
mm = mmap.mmap(f.fileno(), length=0, access=mmap.ACCESS_READ)
reader = HeapReader(mm)
heap = reader.read()
app.logger.info("Loading file finished in %.2f seconds", time.monotonic() - start)

inbound_references = InboundReferences(heap.objects)
retained_heap = provide_retained_heap_with_caching(
args.file, heap, inbound_references
)
if os.environ.get("WERKZEUG_RUN_MAIN") == "true":
start = time.monotonic()
app.logger.info("Loading file %s", args.file)
with open(args.file, "rb") as f:
mm = mmap.mmap(f.fileno(), length=0, access=mmap.ACCESS_READ)
reader = HeapReader(mm)
heap = reader.read()
app.logger.info(
"Loading file finished in %.2f seconds", time.monotonic() - start
)

inbound_references = InboundReferences(heap.objects)
retained_heap = provide_retained_heap_with_caching(
args.file, heap, inbound_references
)

host = os.environ.get("FLASK_SERVER_NAME")
app.run(debug=True, host=host)

0 comments on commit 09e00a4

Please sign in to comment.