简体   繁体   中英

sanic event loop and autobahn

I have a project written with Sanic

application = Sanic(__name__)
...
application.run(host=application.config.HOST, port=application.config.PORT,
                debug=application.config.DEBUG, log_config=application.config.LOG)

and my site has a section for realtime operations

for realtime section i want to use autobahn with sanic loop

and i have custom chat protocol

class ChatServerProtocol(WebSocketServerProtocol):
    ....

and chat factory:

class ChatFactory(WebSocketServerFactory):
    ...

and i get sanic event loop and set factory to ChatFactory

async def after_server_start(app, loop):
    factory = ChatFactory(u"ws://127.0.0.1:9000")
    factory.protocol = ChatServerProtocol

    loop.set_task_factory(factory)

The above code fails:

Error: Bad Request
Traceback (most recent call last):
  File "httptools/parser/parser.pyx", line 296, in httptools.parser.parser.cb_on_message_complete (httptools/parser/parser.c:4868)
File "/Users/XXX/Documents/Projects/sanic_env/lib/python3.6/site-packages/sanic/server.py", line 227, in on_message_complete
self.execute_request_handler()
File "/Users/XXX/Documents/Projects/sanic_env/lib/python3.6/site-packages/sanic/server.py", line 234, in execute_request_handler
self.stream_response))
File "uvloop/loop.pyx", line 1146, in uvloop.loop.Loop.create_task (uvloop/loop.c:24809)
TypeError: __call__() takes 1 positional argument but 3 were given

and Questions :

A: can I use sanic event loop for autobahn?

‌B: If yes, how should it be done?

Hmmm. This is a complicated question to answer. Answer now: not sure.

If you look at autobahn-python it specifies that its for twisted/asyncio, didn't find a mention of uvloop. Although I might add I think swopping uvloop for asyncio should be possible if you look at this discussion with a colleague of mine talking about it in crossbar.io: https://github.com/crossbario/autobahn-python/issues/925 .

According to the above mentioned issue 925 you need to create a new event loop var to tap into uvloop.

Haven't tried it myself so hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM