Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,20 @@ Install using pip::
$ pip install python-snap7

No native libraries or platform-specific dependencies are required — python-snap7 is a pure Python package that works on all platforms.


Async support
=============

An ``AsyncClient`` is available for use with ``asyncio``::

import asyncio
import snap7

async def main():
async with snap7.AsyncClient() as client:
await client.connect("192.168.1.10", 0, 1)
data = await client.db_read(1, 0, 4)
print(data)

asyncio.run(main())
43 changes: 43 additions & 0 deletions doc/API/async_client.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
AsyncClient
===========

The :class:`~snap7.async_client.AsyncClient` provides a native ``asyncio``
interface for communicating with Siemens S7 PLCs. It has feature parity with
the synchronous :class:`~snap7.client.Client` and is safe for concurrent use
via ``asyncio.gather()``.

Quick start
-----------

.. code-block:: python

import asyncio
import snap7

async def main():
async with snap7.AsyncClient() as client:
await client.connect("192.168.1.10", 0, 1)
data = await client.db_read(1, 0, 4)
print(data)

asyncio.run(main())

Concurrent reads
----------------

An internal ``asyncio.Lock`` serialises each send/receive cycle so that
multiple coroutines can safely share a single connection:

.. code-block:: python

results = await asyncio.gather(
client.db_read(1, 0, 4),
client.db_read(1, 10, 4),
)

API reference
-------------

.. automodule:: snap7.async_client
:members:
:exclude-members: AsyncISOTCPConnection
1 change: 1 addition & 0 deletions doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Contents:
development

API/client
API/async_client
API/server
API/partner
API/logo
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Homepage = "https://github.com/gijzelaerr/python-snap7"
Documentation = "https://python-snap7.readthedocs.io/en/latest/"

[project.optional-dependencies]
test = ["pytest", "pytest-cov", "pytest-html", "mypy", "types-setuptools", "ruff", "tox", "tox-uv", "types-click", "uv"]
test = ["pytest", "pytest-asyncio", "pytest-cov", "pytest-html", "mypy", "types-setuptools", "ruff", "tox", "tox-uv", "types-click", "uv"]
cli = ["rich", "click" ]
doc = ["sphinx", "sphinx_rtd_theme"]

Expand Down
2 changes: 2 additions & 0 deletions snap7/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from importlib.metadata import version, PackageNotFoundError

from .client import Client
from .async_client import AsyncClient
from .server import Server
from .partner import Partner
from .logo import Logo
Expand All @@ -16,6 +17,7 @@

__all__ = [
"Client",
"AsyncClient",
"Server",
"Partner",
"Logo",
Expand Down
Loading
Loading