Is it possible to turn docker run
command with detach=True
into an asynchronous task? I am planning on running a continuous application that reads data from Kafka/RabbitMQ and processes it in docker containers. I can do this in a blocking way one-by-one, but my goal is to speed up a bit and run up to 4 tasks simultaneously. This would be a nice implementation with co-routines a asyncio.Semaphore. However, I am not sure how to turn background process into something I can control or read state. Any ideas?
My current minimal example uses while loop with single container
import docker
import json
from time import sleep
client = docker.from_env()
container = client.containers.run("my_container", "my", detach=True)
while True:
container.reload()
if not container.attrs['State']['Running']:
print(container.logs())
break
sleep(1)