python get docker stats code example

Example: python docker stats

import docker
import multiprocessing as mp

output = mp.Queue()

def stats(server):
    client=docker.from_env()
    client_lowlevel = docker.APIClient(base_url='unix://var/run/docker.sock')
    client_stats=client_lowlevel.stats(container=server,decode=True, stream=False)
    output.put(client_stats)

processes =[ mp.Process(target=stats, args=(server)) for server in ['server1','server2']]

# Run processes
for p in processes:
    p.start()

# Exit the completed processes
for p in processes:
    p.join()