实际上,Docker Python SDK运行得很好:https://docker-py.readthedocs.io/en/stable/client.html
但是我尝试同时执行docker exec和asyncio包。似乎不太可能?jenkins怎么能这么做?
asyncio代码:
import asyncio
async def factorial(name, number):
print(number)
await asyncio.sleep(number)
print(name)
async def main():
# Schedule three calls *concurrently*:
L = await asyncio.gather(
factorial("A", 2),
factorial("B", 3),
factorial("C", 4),
)
print(L)
asyncio.run(main())
然后是docker代码:
async def gogo():
client = docker.DockerClient(base_url='unix://var/run/docker.sock')
container = client.containers.create(image_parsed, detach=True, stdin_open=True, tty=True, entrypoint="bash")
container.start()
res = container.exec_run(cmd='bash -c "echo hello stdout ; sleep 3s; echo hello stderr >&2; ls -a"', stream=True, demux=False)
#container.wait()
while True:
try:
print(next(res.output))
except:
break
container.stop()
container.remove()
async def gogo_group():
print(f"started at {time.strftime('%X')}")
L = await asyncio.gather(
gogo(),
gogo()
)
print(L)
print(f"finished at {time.strftime('%X')}")
asyncio.run(gogo_group())
你可以观察到asyncio代码是同时执行的,但docker代码是顺序执行的。有办法解决吗?
由于Paul Cornelius的评论,我更改了代码,但没有帮助:
async def async_wrap(container):
return container.exec_run(cmd='bash -c "echo hello stdout ; sleep 3s; echo hello stderr >&2; ls -a"', stream=True, demux=False)
async def gogo():
client = docker.DockerClient(base_url='unix://var/run/docker.sock')
container = client.containers.create(image_parsed, detach=True, stdin_open=True, tty=True, entrypoint="bash")
container.start()
res = await async_wrap(container)
#container.wait()
while True:
try:
print(next(res.output))
except:
break
container.stop()
container.remove()
async def gogo_group():
print(f"started at {time.strftime('%X')}")
L = await asyncio.gather(
gogo(),
gogo()
)
print(L)
print(f"finished at {time.strftime('%X')}")
1条答案
按热度按时间z9zf31ra1#
多处理是并发地进行的: