Skip to content

Async tcp server#68

Closed
bradder555 wants to merge 5 commits into
AdvancedClimateSystems:masterfrom
bradder555:async_tcp_server
Closed

Async tcp server#68
bradder555 wants to merge 5 commits into
AdvancedClimateSystems:masterfrom
bradder555:async_tcp_server

Conversation

@bradder555

Copy link
Copy Markdown

a blocking server is handy if there is a background thread mutating
memory, or if we are only interested in mutating this memory through modbus

to expand the functionality a little bit, an async option has been added,
this fires up the server on a background thread freeing up the forground
(or alternative thread) to modify the block of memory.

this is nice, because the application can now be used to simulate a plc
or rio in one process

currently this pattern only works with the TcpSocket server, it would be nice
to expand this further to provide the same interface for the serial class

bradder555 and others added 3 commits March 5, 2019 21:43
a blocking server is handy if there is a background thread mutating
memory, or if we are only interested in mutating this memory through modbus

to expand the functionality a little bit, an async option has been added,
this fires up the server on a background thread freeing up the forground
(or alternative thread) to modify the block of memory.

this is nice, because the application can now be used to simulate a plc
or rio in one process

currently this pattern only works with the TcpSocket server, it would be nice
to expand this further to provide the same interface for the serial class
asynchronicity has been added to the modbus tcp server, this adds an
example to the readme to demonstrate how to use these changes
@coveralls

coveralls commented Mar 5, 2019

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.06%) to 96.362% when pulling e8fdae8 on bradder555:async_tcp_server into 0560a42 on AdvancedClimateSystems:master.

the async server is added to the tests in order to maintain test
coverage
2.7 doesn't like the syntax:
    def __init__(self, server_address, request_handler):
      super().__init__(server_address, request_handler)

since i don't need to use super, it's an easy fix to delete these two
lines

@OrangeTux OrangeTux left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your PR! I see the need for non-blocking server, but I don't think adding special support for it is a bit overkill. You can just use the code you wrote in the start_async()method inside the main handler:

# do imports
app = get_server(TCPServer, ('localhost', 502), RequestHandler)

# Configure handlers

if __name__ == '__main__':
    try:
       t = Thread(target=app.serve_forever)
       t.daemon = True
       t.start()

       for i in range(10):
           print(i)
           sleep(1)
    finally
        app.shutdown()
        app.server_close()

        t.join()

This has several advantages:

  • doesn't add complexity to uModbus API
  • work consistent with both RTU and TCP servers

There I won't accept the PR.

On a side note: the term async is often used to refer to asyncio, which is different programming paradigm than threads, which you use in your PR.

@OrangeTux OrangeTux closed this Mar 6, 2019
@bradder555

Copy link
Copy Markdown
Author

words have meaning outside of pythonland:
https://stackoverflow.com/questions/748175/asynchronous-vs-synchronous-execution-what-does-it-really-mean

I can appreciate how the use of async can be confusing

hmaerki pushed a commit to hmaerki/obsolete-fork-uModbus that referenced this pull request Dec 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants