dgbpy.servicemgr

Classes

ServiceMgr

A non-blocking, single-threaded TCP server.

Message

Packet

Module Contents

class dgbpy.servicemgr.ServiceMgr(cmdserver, tornadoport, serviceID=None)

Bases: tornado.tcpserver.TCPServer

A non-blocking, single-threaded TCP server.

To use TCPServer, define a subclass which overrides the handle_stream method. For example, a simple echo server could be defined like this:

from tornado.tcpserver import TCPServer
from tornado.iostream import StreamClosedError

class EchoServer(TCPServer):
    async def handle_stream(self, stream, address):
        while True:
            try:
                data = await stream.read_until(b"\n") await
                stream.write(data)
            except StreamClosedError:
                break

To make this server serve SSL traffic, send the ssl_options keyword argument with an ssl.SSLContext object. For compatibility with older versions of Python ssl_options may also be a dictionary of keyword arguments for the ssl.wrap_socket method.:

ssl_ctx = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
ssl_ctx.load_cert_chain(os.path.join(data_dir, "mydomain.crt"),
                        os.path.join(data_dir, "mydomain.key"))
TCPServer(ssl_options=ssl_ctx)

TCPServer initialization follows one of three patterns:

  1. listen: single-process:

    async def main():
        server = TCPServer()
        server.listen(8888)
        await asyncio.Event.wait()
    
    asyncio.run(main())
    

    While this example does not create multiple processes on its own, when the reuse_port=True argument is passed to listen() you can run the program multiple times to create a multi-process service.

  2. add_sockets: multi-process:

    sockets = bind_sockets(8888)
    tornado.process.fork_processes(0)
    async def post_fork_main():
        server = TCPServer()
        server.add_sockets(sockets)
        await asyncio.Event().wait()
    asyncio.run(post_fork_main())
    

    The add_sockets interface is more complicated, but it can be used with tornado.process.fork_processes to run a multi-process service with all worker processes forked from a single parent. add_sockets can also be used in single-process servers if you want to create your listening sockets in some way other than ~tornado.netutil.bind_sockets.

    Note that when using this pattern, nothing that touches the event loop can be run before fork_processes.

  3. bind/start: simple deprecated multi-process:

    server = TCPServer()
    server.bind(8888)
    server.start(0)  # Forks multiple sub-processes
    IOLoop.current().start()
    

    This pattern is deprecated because it requires interfaces in the asyncio module that have been deprecated since Python 3.10. Support for creating multiple processes in the start method will be removed in a future version of Tornado.

Added in version 3.1: The max_buffer_size argument.

Changed in version 5.0: The io_loop argument has been removed.

cmdserver
cmdhost = None
cmdport = None
serviceID = None
_ioloop = None
_actions
__enter__()
__exit__(exc_type, exc_value, traceback)
static _is_port_in_use(port, local_ip)
_startServer(tornadoport, attempts=20)
_register(port, address)
async handle_stream(stream, address)

Override to handle a new .IOStream from an incoming connection.

This method may be a coroutine; if so any exceptions it raises asynchronously will be logged. Accepting of incoming connections will not be blocked by this coroutine.

If this TCPServer is configured for SSL, handle_stream may be called before the SSL handshake has completed. Use .SSLIOStream.wait_for_handshake if you need to verify the client’s certificate or use NPN/ALPN.

Changed in version 4.2: Added the option for this method to be a coroutine.

_processPacket(inpacket)
addAction(key, action)
sendObject(objkey, jsonobj)
can_connect()
class dgbpy.servicemgr.Message(ioloop=None)
ioloop = None
parseAddress(address)
sendObject(host, port, objkey, jsonobj)
sendObjectToAddress(address, objkey, jsonobj)
sendEvent(host, port, eventstr)
sendEventToAddress(address, eventstr)
async _send(host, port, packet)
class dgbpy.servicemgr.Packet(packet=None)
_curreqid = 0
_reqid = None
_subid = None
jsonheader = None
packet = None
_lock
setIsNewRequest()
setTextPayload(jsonobj)
getTextPayload()
_createPacket(payload)
_json_encode(obj, encoding)
_odhdr_decode()
_json_decode(json_bytes, encoding)