并发执行docker exec

waxmsbnn  于 2023-05-06  发布在  Docker
关注(0)|答案(1)|浏览(147)

实际上,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')}")
z9zf31ra

z9zf31ra1#

多处理是并发地进行的:

import multiprocessing
import time
import os

import docker, json

#res = container.exec_run(cmd='bash -c "echo hello stdout ; echo hello stderr >&2"', stream=True, demux=True)

if __name__ == '__main__':

    def async_wrap(container):
      res = container.exec_run(cmd='bash -c "echo hello stdout ; sleep 3s; echo hello stderr >&2; ls -a"', stream=True, demux=False)
      while True:
        try:
          print(next(res.output))
        except:
          break

    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()

    print(f"started at {time.strftime('%X')}")
    p = multiprocessing.Process(target=async_wrap, args=(container,))
    p.start()

    h = multiprocessing.Process(target=async_wrap, args=(container,))
    h.start()
    h.join()
    p.join()
    print(f"finished at {time.strftime('%X')}")

    container.stop()
    container.remove()

相关问题