-
Hello,
Now, after this function is being called, the
the system complains the following message:
I ran
Any idea why I am getting this message and why the byte written is 0? It was working fine in my local docker image without issues but causing failure in our k8s pod in QA. How can I resolve this issue? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Well, 0 bytes is definitely wrong... after the Perhaps a new request came in and a new
Something like this (untested) might do the trick: class MaybeTracker:
def __init__(self):
self.tracker = None
self.counter = 0
def __enter__(self):
if self.tracker is None:
self.counter += 1
memray_file_path = memray.FileDestination(f"/tmp/output.bin.{self.counter}")
self.tracker = memray.Tracker(destination=memray_file_path, trace_python_allocators=True)
self.tracker.__enter__()
def __exit__(self, *args):
if self.tracker is not None:
self.tracker.__exit__(*args)
self.tracker = None
subprocess.run(["memray", "summary", f"/tmp/output.bin.{self.counter}"])
maybe_tracker = MaybeTracker()
def register_memory_profiler(app: FastAPI):
@app.middleware("http")
async def profile_request(request: Request, call_next):
"""
Profile the memory usage of the current request
"""
memray_file_path = memray.FileDestination('/tmp/output.bin', overwrite=True)
with maybe_tracker:
response = await call_next(request)
return response That's got two important changes relative to your version - first, it doesn't attempt to activate tracking reentrantly. Once one coroutine starts tracking, nothing else will touch the tracker until that coroutine deactivates it. Secondly, instead of overwriting the capture files, it writes sequentially numbered ones. Hopefully those two changes do the trick. |
Beta Was this translation helpful? Give feedback.
Well, 0 bytes is definitely wrong... after the
with
block ends, that file should definitely not be empty (though it can be while tracking is still running).Perhaps a new request came in and a new
Tracker
started, overwriting the file before it could be read??memray.Tracker
sets up global state - there can only be one instance of it active at a time. If a second request comes in before that first one has finished, nothing good is going to happen - I think the failure mode might be that it successfully overwrites the file before it eventually fails to activate tracking, resulting in an empty file being left around.Something like this (untested) might do the trick: