我是Sanic的新手,我正试图让它与电动机一起使用。我确实设法使所有内容都在一个文件中工作,但是,当我在项目结构中尝试时,我会遇到以下问题。
[2018-02-28 17:26:58 +0530] [3720] [ERROR] Traceback (most recent call
last):
File "/usr/local/lib/python3.6/site-packages/sanic/app.py", line 556, in
handle_request
response = await response
File "/usr/src/Python-3.6.4/Lib/asyncio/coroutines.py", line 129, in throw
return self.gen.throw(type, value, traceback)
File "/home/msambare/Documents/Projects/Snippets/Sanic-Motor-
Issue/IAC/src/MyPackgae/REST/user_REST.py", line 42, in post
request.json['last']
File "/usr/src/Python-3.6.4/Lib/asyncio/coroutines.py", line 129, in throw
return self.gen.throw(type, value, traceback)
File "/home/msambare/Documents/Projects/Snippets/Sanic-Motor-
Issue/IAC/src/MyPackgae/DAO/user_DAO.py", line 40, in register_user
result = await db.users.insert_one(serialized_user)
RuntimeError: Task <Task pending coro=<Sanic.handle_request() running at
/usr/local/lib/python3.6/site-packages/sanic/app.py:556> created at
/usr/local/lib/python3.6/site-packages/sanic/server.py:299> got Future
<Future pending cb=[run_on_executor.<locals>._call_check_cancel() at
/usr/local/lib/python3.6/site-
packages/motor/frameworks/asyncio/__init__.py:85]> attached to a different
loop
我做了一些研究,如Sanic的GitHub页面上提到的,尝试了"前服务器块"部分进行DB设置。但是,这在一个文件中工作,而不是我的项目结构。
我的项目结构看起来像:
项目结构
以下是我的代码。我已经以更简单的结构重新创建了这个问题,而不会失去本质。
src/mypackage/model/user.py
class User(object):
def __init__(self, first, last):
self.first = first
self.last = last
src/mypackage/uc/user_uc.py.py
from Model import User
from DAO import User_DAO
class User_UC(object):
def __init__(self):
self._user = None
def create_user(self, first, last):
self._user = User(first, last)
ud = User_DAO()
id = ud.register_user(
{
'first': first,
'last': last
}
)
return id
src/dao/motor_connection.py
import uvloop
import asyncio
import motor.motor_asyncio
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
class Motor_Connection(object):
""" Provides a MongoDB connection and sets the DB to be used.
The class implements the Singleton pattern.
"""
__instance = None
def __new__(cls):
if Motor_Connection.__instance is None:
Motor_Connection.__instance = object.__new__(cls)
Motor_Connection.__instance.client =
motor.motor_asyncio.AsyncIOMotorClient(
'localhost',
27017,
io_loop=asyncio.get_event_loop()
)
Motor_Connection.__instance.db =
Motor_Connection.__instance.client.test_database
return Motor_Connection.__instance.db
src/dao/user_dao.py
from DAO import Motor_Connection
db = Motor_Connection()
class User_DAO(object):
async def register_user(self, serialized_user):
result = await db.users.insert_one(serialized_user)
return result.inserted_id
src/mypackage/rest/user_rest.py
from sanic.views import HTTPMethodView
from sanic.response import text
from UC import User_UC
class User_REST(HTTPMethodView):
async def post(self, request):
user_uc = User_UC()
id = await user_uc.create_user(
request.json['first'],
request.json['last']
)
return text(id)
最后是主要程序...
src/mypackage/main.py
from sanic import Sanic
from DAO import Motor_Connection
from REST import User_REST
app = Sanic()
@app.listener('before_server_start')
def init(sanic, loop):
global db
db = Motor_Connection()
app.add_route(User_REST.as_view(), '/')
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8000, workers=3, debug=True)
我尚未包括任何子包装的 init .py文件。我在这些文件中唯一做的就是将这些类带到子包装级别。
从我能够弄清楚的是,当导入user_rest类时,它似乎在main.py中,又导入user_uc类,该类又导入user_dao类,又导入了Motor_connection类 - 这就是MESS的位置发生。这将创建一个单独的事件循环,该循环不由Sanic共享。
因此,如果我的理解是正确的,那么所有链式导入的类都使用一个事件循环,而理性使用另一个事件循环。我知道我们不能有2个事件循环,但我无法弄清楚要解决的问题需要做什么。
请帮忙。预先感谢。
好。问题已经解决。我必须使用全局变量来解决它。请在下面找到修改的代码以供参考。
main.py
from sanic import Sanic
from DAO import Motor_Connection
from REST import User_REST
import commons
app = Sanic()
@app.listener('before_server_start')
def init(sanic, loop):
commons.db = Motor_Connection()
app.add_route(User_REST.as_view(), '/')
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8000, workers=3, debug=True)
commons.globals.py
db = None
user_dao.py
import commons
class User_DAO(object):
async def register_user(self, serialized_user):
result = await commons.db.users.insert_one(serialized_user)
return result.inserted_id
说明:当电动机连接类是单身顿并返回了同一实例时,由于在导入期间初始化时,Sanic在事件循环中无法处理。必须先初始化理性才能在事件循环中获得处理。
通过使用全局变量并在tre_server_start块中初始化它,Sanic可以获取句柄。现在,当您在DAO类中使用相同的变量时,您可以访问Sanic的事件循环。