Quart(异步Flask)应用程序中的Autobahn Websocket客户端

时间:2019-03-28 17:10:11

标签: websocket python-asyncio autobahn crossbar quart

大家晚上好。我对这个地方并不陌生,但最终决定注册并寻求帮助。我使用Quart框架(异步烧瓶)开发了一个Web应用程序。现在,随着应用程序变得越来越大,越来越复杂,我决定将不同的过程分离到不同的服务器实例,这主要是因为我想保持Web服务器的整洁,更加抽象并且没有计算负荷。
因此,我计划将一个Web服务器与几个(如果需要)相同的过程服务器一起使用。目前,所有服务器均基于夸脱框架,仅为了简化开发。我决定使用Crossbar.io路由器和高速公路将所有服务器连接在一起。

在这里出现了问题。 我关注了这篇文章:

Running several ApplicationSessions non-blockingly using autbahn.asyncio.wamp

How can I implement an interactive websocket client with autobahn asyncio?

How I can integrate crossbar client (python3,asyncio) with tkinter

How to send Autobahn/Twisted WAMP message from outside of protocol?

似乎我尝试了所有可能的方法在quart应用程序中实现autobahn websocket客户端。我不知道如何使这一切都可行,无论Quart应用程序是否正常运行,但autobahn WS客户端均无效,反之亦然。

简化后的夸脱应用程序如下:

from quart import Quart, request, current_app
from config import Config
# Autobahn
import asyncio
from autobahn import wamp
from autobahn.asyncio.wamp import ApplicationSession, ApplicationRunner

import concurrent.futures

class Component(ApplicationSession):
    """
    An application component registering RPC endpoints using decorators.
    """

    async def onJoin(self, details):

        # register all methods on this object decorated with "@wamp.register"
        # as a RPC endpoint
        ##
        results = await self.register(self)
        for res in results:
            if isinstance(res, wamp.protocol.Registration):
                # res is an Registration instance
                print("Ok, registered procedure with registration ID {}".format(res.id))
            else:
                # res is an Failure instance
                print("Failed to register procedure: {}".format(res))

    @wamp.register(u'com.mathservice.add2')
    def add2(self, x, y):
        return x + y


def create_app(config_class=Config):

    app = Quart(__name__)
    app.config.from_object(config_class)

    # Blueprint registration
    from app.main import bp as main_bp
    app.register_blueprint(main_bp)

    print ("before autobahn start")
    with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
        runner = ApplicationRunner('ws://127.0.0.1:8080 /ws', 'realm1')
        future = executor.submit(runner.run(Component))
    print ("after autobahn started")

    return app


from app import models

在这种情况下,应用程序陷入了流转循环,并且整个应用程序无法运行(无法处理请求),只有在我通过Ctrl-C中断流转(高速公路)循环时才有可能。

启动后的CMD:

(quart-app) user@car:~/quart-app$ hypercorn --debug --error-log - --access-log - -b 0.0.0.0:8001 tengine:app
Running on 0.0.0.0:8001 over http (CTRL + C to quit)
before autobahn start
Ok, registered procedure with registration ID 4605315769796303

在按下ctrl-C之后:

...
^Cafter autobahn started
2019-03-29T01:06:52 <Server sockets=[<socket.socket fd=11, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=('0.0.0.0', 8001)>]> is serving

如何以无阻塞方式与高速公路客户端一起使用夸脱应用程序?因此,高速公路将打开并保持websocket与Crossbar路由器的连接,并在后台静默收听。

1 个答案:

答案 0 :(得分:0)

好吧,经过许多不眠之夜,我终于找到了解决这个难题的好方法。

感谢这篇文章C-Python asyncio: running discord.py in a thread

因此,我像这样重写了我的代码,并且能够在内部带有高速公路客户端的情况下运行Quart应用程序,并且两者都以非阻塞方式积极工作。 整个__init__.py如下:

from quart import Quart, request, current_app
from config import Config


def create_app(config_class=Config):

    app = Quart(__name__)
    app.config.from_object(config_class)

    # Blueprint registration
    from app.main import bp as main_bp
    app.register_blueprint(main_bp)

    return app


# Autobahn
import asyncio
from autobahn import wamp
from autobahn.asyncio.wamp import ApplicationSession, ApplicationRunner
import threading


class Component(ApplicationSession):
    """
    An application component registering RPC endpoints using decorators.
    """

    async def onJoin(self, details):

        # register all methods on this object decorated with "@wamp.register"
        # as a RPC endpoint
        ##
        results = await self.register(self)
        for res in results:
            if isinstance(res, wamp.protocol.Registration):
                # res is an Registration instance
                print("Ok, registered procedure with registration ID {}".format(res.id))
            else:
                # res is an Failure instance
                print("Failed to register procedure: {}".format(res))


    def onDisconnect(self):
        print('Autobahn disconnected')

    @wamp.register(u'com.mathservice.add2')
    def add2(self, x, y):
        return x + y


async def start():
    runner = ApplicationRunner('ws://127.0.0.1:8080/ws', 'realm1')
    await runner.run(Component) # use client.start instead of client.run

def run_it_forever(loop):
    loop.run_forever()

asyncio.get_child_watcher() # I still don't know if I need this method. It works without it.
loop = asyncio.get_event_loop()
loop.create_task(start())
print('Starting thread for Autobahn...')
thread = threading.Thread(target=run_it_forever, args=(loop,))
thread.start()
print ("Thread for Autobahn has been started...")


from app import models

在这种情况下,我们使用高速公路bruner.run创建任务并将其附加到当前循环,然后在新线程中永久运行此循环。

我对当前的解决方案非常满意....但是后来发现该解决方案有一些缺点,这对我来说至关重要,例如:如果连接断开(例如,交叉开关路由器不可用),请重新连接。使用这种方法,如果连接初始化失败或过一会儿掉线,它将不会尝试重新连接。另外对我来说,如何使用ApplicationSession API(即从我的quart应用程序中的代码注册/调用RPC)还不是很清楚。

幸运的是,我发现了高速公路上在其文档中使用的另一个新组件API: https://autobahn.readthedocs.io/en/latest/wamp/programming.html#registering-procedures https://github.com/crossbario/autobahn-python/blob/master/examples/asyncio/wamp/component/backend.py

它具有自动重新连接功能,使用装饰器@component.register('com.something.do')可以很容易地为RPC注册函数,您只需要在import component之前。

这是__init__.py解决方案的最终视图:

from quart import Quart, request, current_app
from config import Config

def create_app(config_class=Config):
    ...
    return app

from autobahn.asyncio.component import Component, run
from autobahn.wamp.types import RegisterOptions
import asyncio
import ssl
import threading


component = Component(
    transports=[
        {
            "type": "websocket",
            "url": u"ws://localhost:8080/ws",
            "endpoint": {
                "type": "tcp",
                "host": "localhost",
                "port": 8080,
            },
            "options": {
                "open_handshake_timeout": 100,
            }
        },
    ],
    realm=u"realm1",
)

@component.on_join
def join(session, details):
    print("joined {}".format(details))

async def start():
    await component.start() #used component.start() instead of run([component]) as it's async function

def run_it_forever(loop):
    loop.run_forever()

loop = asyncio.get_event_loop()
#asyncio.get_child_watcher() # I still don't know if I need this method. It works without it.
asyncio.get_child_watcher().attach_loop(loop)
loop.create_task(start())
print('Starting thread for Autobahn...')
thread = threading.Thread(target=run_it_forever, args=(loop,))
thread.start()
print ("Thread for Autobahn has been started...")


from app import models

我希望它将对某人有所帮助。干杯!