Skip to content

Commit

Permalink
Update asyncio usage patterns in the doc examples
Browse files Browse the repository at this point in the history
  • Loading branch information
achimnol committed May 7, 2024
1 parent 15a3290 commit 411e0b4
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 168 deletions.
75 changes: 37 additions & 38 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ AsyncIO bindings for docker.io
:target: https://pypi.org/project/aiodocker/
:alt: Python Versions

.. image:: https://travis-ci.com/aio-libs/aiodocker.svg?branch=master
:target: https://travis-ci.com/aio-libs/aiodocker
:alt: Build Status
.. image:: https://github.com/aio-libs/aiodocker/workflows/CI/badge.svg
:target: https://github.com/aio-libs/aiodocker/actions?query=workflow%3ACI
:alt: GitHub Actions status for the main branch

.. image:: https://codecov.io/gh/aio-libs/aiodocker/branch/master/graph/badge.svg
:target: https://codecov.io/gh/aio-libs/aiodocker
Expand Down Expand Up @@ -43,38 +43,37 @@ Examples

.. code-block:: python
import asyncio
import aiodocker
async def list_things():
docker = aiodocker.Docker()
print('== Images ==')
for image in (await docker.images.list()):
tags = image['RepoTags'][0] if image['RepoTags'] else ''
print(image['Id'], tags)
print('== Containers ==')
for container in (await docker.containers.list()):
print(f" {container._id}")
await docker.close()
async def run_container():
docker = aiodocker.Docker()
print('== Running a hello-world container ==')
container = await docker.containers.create_or_replace(
config={
'Cmd': ['/bin/ash', '-c', 'echo "hello world"'],
'Image': 'alpine:latest',
},
name='testing',
)
await container.start()
logs = await container.log(stdout=True)
print(''.join(logs))
await container.delete(force=True)
await docker.close()
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(list_things())
loop.run_until_complete(run_container())
loop.close()
import asyncio
import aiodocker
async def list_things(docker):
print('== Images ==')
for image in (await docker.images.list()):
tags = image['RepoTags'][0] if image['RepoTags'] else ''
print(image['Id'], tags)
print('== Containers ==')
for container in (await docker.containers.list()):
print(f" {container._id}")
async def run_container(docker):
print('== Running a hello-world container ==')
container = await docker.containers.create_or_replace(
config={
'Cmd': ['/bin/ash', '-c', 'echo "hello world"'],
'Image': 'alpine:latest',
},
name='testing',
)
await container.start()
logs = await container.log(stdout=True)
print(''.join(logs))
await container.delete(force=True)
async def main():
docker = aiodocker.Docker()
await list_things(docker)
await run_container(docker)
await docker.close()
if __name__ == "__main__":
asyncio.run(main())
49 changes: 25 additions & 24 deletions docs/configs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,45 +11,46 @@ Create a config
import asyncio
import aiodocker
docker = aiodocker.Docker()
async def create_config():
async def create_config(docker):
config = await docker.configs.create(
name="my_config",
data="This is my config data"
data="This is my config data",
)
await docker.close()
return config
async def create_service(TaskTemplate):
async def create_service(docker, task_template):
service = await docker.services.create(
task_template=TaskTemplate,
name="my_service"
task_template=task_template,
name="my_service",
)
await docker.close()
return service
if __name__ == '__main__':
loop = asyncio.get_event_loop()
my_config = loop.run_until_complete(create_config())
TaskTemplate = {
async def main():
docker = aiodocker.Docker()
my_config = await create_config(docker)
task_template = {
"ContainerSpec": {
"Image": "redis",
"Configs": [
{
"File": {
"Name": my_config["Spec"]["Name"],
"UID": "0",
"GID": "0",
"Mode": 292
{
"File": {
"Name": my_config["Spec"]["Name"],
"UID": "0",
"GID": "0",
"Mode": 292
},
"ConfigID": my_config["ID"],
"ConfigName": my_config["Spec"]["Name"],
},
"ConfigID": my_config["ID"],
"ConfigName": my_config["Spec"]["Name"],
}
],
},
}
loop.run_until_complete(create_service(TaskTemplate))
loop.close()
service = await create_service(docker, task_template)
print(service)
await docker.close()
if __name__ == "__main__":
asyncio.run(main())
------------
Expand Down
46 changes: 21 additions & 25 deletions docs/containers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,27 @@ Create a container

.. code-block:: python
import asyncio
import aiodocker
docker = aiodocker.Docker()
config = {
"Cmd": ["/bin/ls"],
"Image": "alpine:latest",
"AttachStdin": False,
"AttachStdout": False,
"AttachStderr": False,
"Tty": False,
"OpenStdin": False,
}
async def create_container():
container = await docker.containers.create(config=config)
print(container)
await docker.close()
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(create_container())
loop.close()
import asyncio
import aiodocker
config = {
"Cmd": ["/bin/ls"],
"Image": "alpine:latest",
"AttachStdin": False,
"AttachStdout": False,
"AttachStderr": False,
"Tty": False,
"OpenStdin": False,
}
async def create_container():
docker = aiodocker.Docker()
container = await docker.containers.create(config=config)
print(container)
await docker.close()
if __name__ == "__main__":
asyncio.run(create_container())
---------
Reference
Expand Down
69 changes: 34 additions & 35 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,41 +44,40 @@ Examples

.. code-block:: python
import asyncio
import aiodocker
async def list_things():
docker = aiodocker.Docker()
print('== Images ==')
for image in (await docker.images.list()):
tags = image['RepoTags'][0] if image['RepoTags'] else ''
print(image['Id'], tags)
print('== Containers ==')
for container in (await docker.containers.list()):
print(f" {container._id}")
await docker.close()
async def run_container():
docker = aiodocker.Docker()
print('== Running a hello-world container ==')
container = await docker.containers.create_or_replace(
config={
'Cmd': ['/bin/ash', '-c', 'echo "hello world"'],
'Image': 'alpine:latest',
},
name='testing',
)
await container.start()
logs = await container.log(stdout=True)
print(''.join(logs))
await container.delete(force=True)
await docker.close()
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(list_things())
loop.run_until_complete(run_container())
loop.close()
import asyncio
import aiodocker
async def list_things(docker):
print('== Images ==')
for image in (await docker.images.list()):
tags = image['RepoTags'][0] if image['RepoTags'] else ''
print(image['Id'], tags)
print('== Containers ==')
for container in (await docker.containers.list()):
print(f" {container._id}")
async def run_container(docker):
print('== Running a hello-world container ==')
container = await docker.containers.create_or_replace(
config={
'Cmd': ['/bin/ash', '-c', 'echo "hello world"'],
'Image': 'alpine:latest',
},
name='testing',
)
await container.start()
logs = await container.log(stdout=True)
print(''.join(logs))
await container.delete(force=True)
async def main():
docker = aiodocker.Docker()
await list_things(docker)
await run_container(docker)
await docker.close()
if __name__ == "__main__":
asyncio.run(main())
Source code
Expand Down
2 changes: 1 addition & 1 deletion docs/networks.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Create a network
print(network)
await docker.close()
if __name__ == '__main__':
if __name__ == "__main__":
asyncio.run(create_network())
---------
Expand Down
45 changes: 23 additions & 22 deletions docs/secrets.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,45 +11,46 @@ Create a secret
import asyncio
import aiodocker
docker = aiodocker.Docker()
async def create_secret():
async def create_secret(docker):
secret = await docker.secrets.create(
name="my_secret",
data="you can not read that terrible secret"
)
await docker.close()
return secret
async def create_service(TaskTemplate):
async def create_service(docker, task_template):
service = await docker.services.create(
task_template=TaskTemplate,
task_template=task_template,
name="my_service"
)
await docker.close()
return service
if __name__ == '__main__':
loop = asyncio.get_event_loop()
my_secret = loop.run_until_complete(create_secret())
TaskTemplate = {
async def main():
docker = aiodocker.Docker()
my_secret = await create_secret(docker)
task_template = {
"ContainerSpec": {
"Image": "redis",
"Secrets": [
{
"File": {
"Name": my_secret["Spec"]["Name"],
"UID": "0",
"GID": "0",
"Mode": 292
{
"File": {
"Name": my_secret["Spec"]["Name"],
"UID": "0",
"GID": "0",
"Mode": 292
},
"SecretID": my_secret["ID"],
"SecretName": my_secret["Spec"]["Name"]
},
"SecretID": my_secret["ID"],
"SecretName": my_secret["Spec"]["Name"]
}
],
},
}
loop.run_until_complete(create_service(TaskTemplate))
loop.close()
service = await create_service(docker, task_template)
print(service)
await docker.close()
if __name__ == "__main__":
asyncio.run(main())
------------
Expand Down
Loading

0 comments on commit 411e0b4

Please sign in to comment.