Closed hieu-n closed 6 years ago
Try this:
import asyncio
from japronto import Application
from motor.motor_asyncio
def create_app():
app = Application()
event_loop = asyncio.get_event_loop()
client = motor.motor_asyncio.AsyncIOMotorClient(io_loop=event_loop)
db = client.mydb
async def index(request):
result = await db.find_one()
print(result)
return request.Response(text=str(result), mime_type='text/html')
app.router.add_route('/', index)
@vladz Thank you for your response. I tried and still got RuntimeError: Task ... got Future <Future pending cb=[_chain_future.<locals>._call_check_cancel() at ... attached to a different loop
.
And if you change the code for this one?
from japronto import Application
from motor.motor_asyncio
def create_app():
app = Application()
event_loop = app.loop # <<--
client = motor.motor_asyncio.AsyncIOMotorClient(io_loop=event_loop)
db = client.mydb
async def index(request):
result = await db.find_one()
print(result)
return request.Response(text=str(result), mime_type='text/html')
app.router.add_route('/', index)
app.run()
The idea is to catch a event_loop from japronto and use it in motor.
What you are doing will result in undefined behavior because Japronto is a pre-forking server and if you set your loop up in the parent process and then fork things will go terribly wrong. There is no official API to do it at the moment.
If you really want to hack you can override: https://github.com/squeaky-pl/japronto/blob/master/src/japronto/app/__init__.py#L159. This function is called from new process already, you can grab the loop after this is configured and setup your mongodb driver there.
@squeaky-pl Thank you for comments.
My app structure (simplified):
app/__init__.py
manage.py
When I ran it, I got an exception:
I suspect putting
client = motor.motor_asyncio.AsyncIOMotorClient()
insideindex
would work, but that would create new connections for every request. Reusing connection would be more preferable.