Add native AsyncClient with asyncio support#593
Open
gijzelaerr wants to merge 4 commits intomasterfrom
Open
Conversation
This reverts commit 746a9b2.
3 tasks
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- Add pytest-asyncio to test dependencies in pyproject.toml - Apply ruff format to async_client.py (struct.pack arg formatting) - Add AsyncClient documentation (doc/API/async_client.rst) - Add async_client to Sphinx toctree - Add async example to README.rst Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds a native
AsyncClientfor async S7 communication usingasynciostreams. This replaces the previousasyncio.to_thread()approach (which was not safe for concurrent use) with proper non-blocking I/O, following the pattern from python-s7comm.Key design decisions
asyncio.open_connection()withStreamReader/StreamWriterinstead of blocking sockets wrapped in threads. The event loop is never blocked.asyncio.Lock()in_send_receive(): S7 is request-response, so each send+receive cycle must be atomic. The lock serializes concurrent coroutines, makingasyncio.gather()safe.ClientMixinfor shared logic: 14 pure-computation methods (parameter management, error text, area mapping, etc.) are shared betweenClientandAsyncClientvia a mixin insnap7/client_base.py, eliminating code duplication.S7Protocolunchanged: The protocol layer is pure computation (struct pack/unpack) — no I/O, no async needed.New files
snap7/async_client.py—AsyncISOTCPConnection(async transport) andAsyncClient(full-featured async S7 client)snap7/client_base.py—ClientMixinwith shared pure-computation methodstests/test_async_client.py— 26 tests covering connection, DB read/write, area read/write, concurrent safety, multi-var operations, and synchronous helpersFeature parity
AsyncClientsupports the same operations as the syncClient:connect/disconnect/get_connecteddb_read/db_write/db_getread_area/write_area(with chunked transfers for large payloads)ab_read/ab_write,eb_read/eb_write,mb_read/mb_write,tm_read/tm_write,ct_read/ct_writeread_multi_vars/write_multi_varslist_blocks/list_blocks_of_type/get_block_infoget_cpu_state/get_cpu_info/get_pdu_lengthupload/full_upload/downloadread_szl/get_plc_datetime/set_plc_datetimeplc_hot_start/plc_cold_start/plc_stopget_cp_info/get_order_code/get_protectionasync withcontext managerTest plan
asyncio.gather()with multiple reads, mixed read/write, and 10 concurrent reads🤖 Generated with Claude Code