我有一个使用Sanic编写的项目。
application = Sanic(__name__)
...
application.run(host=application.config.HOST, port=application.config.PORT,
debug=application.config.DEBUG, log_config=application.config.LOG)
我的网站有一个实时操作部分。
对于实时操作部分,我想使用与sanic循环兼容的autobahn。
并且我有一个自定义聊天协议。
class ChatServerProtocol(WebSocketServerProtocol):
....
还有聊天工厂:
class ChatFactory(WebSocketServerFactory):
...
我获取了Sanic事件循环并将工厂设置为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)
上述代码失败了:
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
问题:
A: 我能在Autobahn中使用sanic事件循环吗?
B: 如果可以,应该如何操作?