New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Lists not returned with __iter__ and not compatible with an async for loop #491
Comments
Tried changing things around some but still am not having any luck: @router.websocket("/stats")
async def dashboard(websocket: WebSocket):
# auth_success = await websocket_auth(websocket=websocket)
# if auth_success:
await websocket.accept()
tasks = []
async with aiodocker.Docker() as docker:
containers = await docker.containers.list()
for app in containers:
_name = app._container['Names'][0][1:]
container: DockerContainer = await docker.containers.get(_name)
stats = container.stats(stream=True)
tasks.append(process_container(_name, stats))
await asyncio.gather(*tasks)
async def process_container(name, stats):
cpu_total = 0.0
cpu_system = 0.0
cpu_percent = 0.0
async for line in stats:
mem_current = line["memory_stats"]["usage"]
mem_total = line["memory_stats"]["limit"]
try:
cpu_percent, cpu_system, cpu_total = await calculate_cpu_percent2(line, cpu_total, cpu_system)
except KeyError as e:
print("error while getting new CPU stats: %r, falling back")
cpu_percent = await calculate_cpu_percent(line)
full_stats = {
"name": _name,
"time": line['read'],
"cpu_percent": cpu_percent,
"mem_current": mem_current,
"mem_total": line["memory_stats"]["limit"],
"mem_percent": (mem_current / mem_total) * 100.0,
}
await websocket.send_text(json.dumps(full_stats))
|
This fixed it (indentation error) @router.websocket("/stats")
async def dashboard(websocket: WebSocket):
# auth_success = await websocket_auth(websocket=websocket)
# if auth_success:
await websocket.accept()
tasks = []
async with aiodocker.Docker() as docker:
containers = await docker.containers.list()
for app in containers:
_name = app._container['Names'][0][1:]
container: DockerContainer = await docker.containers.get(_name)
stats = container.stats(stream=True)
tasks.append(process_container(_name, stats, websocket))
await asyncio.gather(*tasks)
async def process_container(name, stats, websocket):
cpu_total = 0.0
cpu_system = 0.0
cpu_percent = 0.0
async for line in stats:
mem_current = line["memory_stats"]["usage"]
mem_total = line["memory_stats"]["limit"]
try:
cpu_percent, cpu_system, cpu_total = await calculate_cpu_percent2(line, cpu_total, cpu_system)
except KeyError as e:
print("error while getting new CPU stats: %r, falling back")
cpu_percent = await calculate_cpu_percent(line)
full_stats = {
"name": name,
"time": line['read'],
"cpu_percent": cpu_percent,
"mem_current": mem_current,
"mem_total": line["memory_stats"]["limit"],
"mem_percent": (mem_current / mem_total) * 100.0,
}
await websocket.send_text(json.dumps(full_stats)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Long story short
I'm attempting to gather stats on all of my running docker containers then sending them (via a websocket) back to my frontend. I can't get an async for loop going using docker.containers.list() as it returns a standard list without an aiter method.
async for
loopHow to reproduce (Ignore the fastapi/websocket stuff)
Running the following (without websocket/fastapi stuff) will only stream stats for a single container (if you remove async from the first for loop), not all of them at the same time.
Your environment
Running the latest version of Manjaro, Python 3.8.3, FastAPI. Not sure if anything in my env is going to be useful but if you need anything specifically feel free to ask.
The text was updated successfully, but these errors were encountered: