作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在浏览aiodocker库。
并且文档表明他们观看python-asyncio
标签。所以我想问一下如何异步我的Docker代码,因为我无法从文档和源代码中弄清楚。这是我需要异步执行的代码(下面的detach=True
无法使用,因为有时容器以非零状态代码退出。使它们异步可以帮助我更好地处理此问题。):
import docker
def synchronous_request(url):
client = docker.from_env()
local_dir = '/home/ubuntu/git/docker-scraper/data'
volumes = {local_dir: {'bind': '/download/', 'mode': 'rw'}}
environment = {'URL': url}
client.containers.run('wgettor:latest', auto_remove=True, volumes=volumes, environment=environment)
aiodocker
是:
import aiodocker
async def make_historical_request(url):
docker = await aiodocker.Docker()
client = await aiodocker.DockerContainers(docker)
local_dir = '/home/ubuntu/git/docker-scraper/data'
volumes = {local_dir: {'bind': '/download/', 'mode': 'rw'}}
environment = {'URL': url}
await client.run(config={"auto_remove": "True",
"volumes": volumes,
"environment": environment}, name="wgettor:latest")
import docker
import asyncio
from collections import namedtuple
URL = namedtuple('URL', 'val')
URLs = (
URL('https://www.google.com'),
URL('https://www.yahoo.com')
)
client = docker.from_env()
local_dir = '/home/ubuntu/git/docker-scraper/data-test'
volumes = {local_dir: {'bind': '/download/', 'mode': 'rw'}}
async def run_container(client, volumes, environment, *, pid):
print("Starting the container on pid: {}".format(pid))
return client.containers.run('wgettor:latest', auto_remove=True, detach=True,
volumes=volumes, environment=environment)
async def make_historical_request(url, *, pid):
print("Starting the retrieval of: {}, on pid: {}".format(url, pid))
environment = {'URL': url}
return await run_container(client, volumes, environment, pid=pid)
async def main():
tasks = [asyncio.ensure_future(make_historical_request(url.val, pid=ix)) for ix, url in enumerate(URLs)]
await asyncio.wait(tasks)
if __name__ == '__main__':
asyncio.run(main())
async def run_container(docker, url):
config = {
'Env': ["URL="+url],
'HostConfig': {
'Binds': local_dir + ":" + "/download/"
}
}
try:
await asyncio.sleep(random.random() * 0.001)
container = await docker.containers.create_or_replace(
config=config,
name="wgettor:latest",
)
await container.start()
await container.kill()
return url
except DockerError as err:
print(f'Error starting wgettor:latest, container: {err}')
async def main():
start = time.time()
docker = Docker()
futures = [run_container(docker, url) for url in URLs]
# futures = [fetch_async(i) for i in range(1, MAX_CLIENTS + 1)]
for i, future in enumerate(asyncio.as_completed(futures)):
result = await future
print('{} {}'.format(">>" * (i + 1), result))
print("Process took: {:.2f} seconds".format(time.time() - start))
最佳答案
async def run_container(client, volumes, environment, *, pid):
print('Starting the container on pid: {}'.format(pid))
return client.containers.run(..)
import aiodocker
async def run_container(docker, name, config):
try:
container = await docker.containers.create_or_replace(
config=config,
name=name,
)
await container.start()
return container
except DockerError as err:
print(f'Error starting {name} container: {err}')
from aiodocker import Docker
docker = Docker()
config = {}
loop.run_until_complete(run_container(docker, ..., config)
config = {
'Image': 'imagename',
'HostConfig': {'Binds':['/local_path:/container_path']},
}
关于python-3.x - aiodocker异步创建容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53085421/
我是一名优秀的程序员,十分优秀!