-
Notifications
You must be signed in to change notification settings - Fork 1k
Trio support #583
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Trio support #583
Changes from all commits
Commits
Show all changes
41 commits
Select commit
Hold shift + click to select a range
e33153f
it reads! (something)
altendky 9320e25
read a few things in tc.py
altendky e7a365b
add a few tests
altendky 29346ff
commented out attempt to break the framer does
altendky 809972d
slight simplification
altendky 743bda1
hacky but running trio server
altendky 4123eb4
misc
altendky e3be360
missing imports
altendky b0e72eb
use async_generator for asynccontextmanager
altendky 0229932
Add async_generator to extras_require:trio
altendky a844e30
use https for github link
altendky a4c5e71
another test
altendky 4187d5f
pytest-trio~=0.7.0;python_version>="3.6"
altendky 1a5ee53
misc
altendky 83bf250
misc
altendky 12af789
Merge branch 'dev' into trio
altendky 12c3313
Merge branch 'dev' into trio
altendky d514d08
update test for logging change
altendky 7bd1cbb
skip trio tests in py2
altendky 325a53e
unit test trio.execute()
altendky a2e368f
simplify trio broadcast handling code
altendky 023caff
test trio.incoming
altendky b1fc236
shift examples to the examples directory
altendky 707035e
first round of trio documentation additions
altendky 61dc891
more docstrings and some refactors
altendky 6f1af2f
simplify trio client test file
altendky ffd12d1
more tests and use mock
altendky ef8ffbf
stop using MagicMock and AsyncMock
altendky 49f7ca9
trio.BaseModbusAsyncClientProtocol tests
altendky 5464580
more tests...
altendky 343346c
fix the tests
altendky 1207589
even more tests
altendky 8774d1b
one more assert
altendky c750233
fixup py36-37
altendky c47a533
fixup patching and mocking of trio.open_tcp_stream()
altendky 363903e
use outcome so we can handle errors as well
altendky f08b86c
handle protocol connection lost
altendky c6db326
explain the +1s
altendky bcef8b7
use a lock for trio client sending
altendky 0528b1c
Merge branch 'dev' into trio
altendky ab584ea
Merge branch 'dev' into trio
altendky File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| ================================================== | ||
| Async Trio Client Example | ||
| ================================================== | ||
| .. literalinclude:: ../../../examples/common/async_trio_client.py |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| ================================================== | ||
| Async Trio Server Example | ||
| ================================================== | ||
| .. literalinclude:: ../../../examples/common/trio_server.py |
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| pymodbus\.client\.asynchronous\.trio package | ||
| =============================================== | ||
|
|
||
| .. automodule:: pymodbus.client.asynchronous.trio | ||
| :members: | ||
| :undoc-members: | ||
| :show-inheritance: | ||
|
|
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| #!/usr/bin/env python | ||
| """ | ||
| Pymodbus Asynchronous Client Examples | ||
| -------------------------------------------------------------------------- | ||
|
|
||
| The following is an example of how to use the asynchronous modbus | ||
| client implementation from pymodbus with Trio. | ||
|
|
||
| The example is only valid on Python3.6 and above | ||
| """ | ||
| import contextlib | ||
| import logging | ||
| import sys | ||
|
|
||
| from pymodbus.client.asynchronous.tcp import AsyncModbusTCPClient as ModbusClient | ||
| from pymodbus.client.asynchronous import schedulers | ||
|
|
||
| import trio | ||
|
|
||
|
|
||
| async def main(): | ||
| root_logger = logging.getLogger() | ||
| handler = logging.StreamHandler(stream=sys.stdout) | ||
| root_logger.addHandler(hdlr=handler) | ||
| root_logger.setLevel(logging.DEBUG) | ||
|
|
||
| client = ModbusClient(scheduler=schedulers.TRIO, host="127.0.0.1", port=5020) | ||
|
|
||
| with contextlib.suppress(KeyboardInterrupt): | ||
| async with client.manage_connection() as protocol: | ||
| while True: | ||
| response = await protocol.read_coils(address=1, count=1, unit=0x01) | ||
| print(' response:', response.bits) | ||
| response = await protocol.read_holding_registers(address=1, count=1, unit=0x01) | ||
| print(' response:', response.registers) | ||
| response = await protocol.read_holding_registers(address=10, count=1, unit=0x01) | ||
| print(' response:', response.registers) | ||
|
|
||
| await trio.sleep(1) | ||
|
|
||
|
|
||
| trio.run(main) |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| #!/usr/bin/env python | ||
| """ | ||
| Pymodbus Trio Server Example | ||
| -------------------------------------------------------------------------- | ||
|
|
||
| """ | ||
| import logging | ||
|
|
||
| FORMAT = ( | ||
| "%(asctime)-15s %(threadName)-15s" | ||
| " %(levelname)-8s %(module)-15s:%(lineno)-8s %(message)s" | ||
| ) | ||
| logging.basicConfig(format=FORMAT) | ||
| log = logging.getLogger() | ||
| log.setLevel(logging.DEBUG) | ||
|
|
||
| import functools | ||
|
|
||
| from pymodbus.server.trio import tcp_server | ||
| from pymodbus.device import ModbusDeviceIdentification | ||
| from pymodbus.datastore import ModbusSequentialDataBlock | ||
| from pymodbus.datastore import ModbusSlaveContext, ModbusServerContext | ||
|
|
||
| import trio | ||
|
|
||
|
|
||
| async def run_server(): | ||
| store = ModbusSlaveContext( | ||
| di=ModbusSequentialDataBlock(0, [17] * 100), | ||
| co=ModbusSequentialDataBlock(0, [17] * 100), | ||
| hr=ModbusSequentialDataBlock(0, [17] * 100), | ||
| ir=ModbusSequentialDataBlock(0, [17] * 100), | ||
| ) | ||
|
|
||
| context = ModbusServerContext(slaves=store, single=True) | ||
|
|
||
| identity = ModbusDeviceIdentification() | ||
| identity.VendorName = "Pymodbus" | ||
| identity.ProductCode = "PM" | ||
| identity.VendorUrl = "https://github.com/riptideio/pymodbus/" | ||
| identity.ProductName = "Pymodbus Server" | ||
| identity.ModelName = "Pymodbus Server" | ||
| identity.MajorMinorRevision = "2.3.0" | ||
|
|
||
| await trio.serve_tcp( | ||
| functools.partial( | ||
| tcp_server, | ||
| context=context, | ||
| identity=identity, | ||
| ), | ||
| port=5020, | ||
| host="0.0.0.0", | ||
| ) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| trio.run(run_server) |
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,3 +7,4 @@ | |
| REACTOR = "reactor" | ||
| IO_LOOP = "io_loop" | ||
| ASYNC_IO = "async_io" | ||
| TRIO = "trio" | ||
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is inconsistent with other factories which return two values.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They do return two values, but I don't see the consistency in the meaning of those two values. Can you help me understand what sort of thing the first value should be and what the second should be? Then I can try to map that into the Trio stuff.
Twisted: protocol / deferred
Tornado: protocol / future
asyncio: event loop / client
Trio: ? / ?
I can see the similarity between Twisted and Tornado. I don't see how asyncio fits. Apparently when I coded this a couple months ago I didn't see what other useful thing I could return in the Trio case. We'll see if I can find something now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Relatedly, the following docstring seems to only get asyncio right.
https://github.com/riptideio/pymodbus/blob/c7a582b6796be211d693dba80367f2b26846c134/pymodbus/client/asynchronous/__init__.py#L15-L22