Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 3.6 - Sanic Motorengine

I am trying to setup the new Sanic web framework (which is promised to be extremely fast) with motorengine in order to achieve 100% async.

My setup so far:

app = Sanic(__name__)

@app.listener('after_server_start')
async def setup_dbconn(app, loop):
    connect("database_name", username="user", password="pass", host="192.168.1.200", port=27017, io_loop=asyncio.get_event_loop())

Unfortunately I get:

motorengine.connection.ConnectionError: Cannot connect to database default :
Unknown option username

Why does this crash?

like image 985
Creative crypter Avatar asked Dec 02 '25 21:12

Creative crypter


1 Answers

The specific problem you encounter is not about Sanic but has to do with motorengine.

Use the database URI to connect:

motorengine.connect(
    db=name, 
    alias=alias, 
    host="mongodb://username:password@localhost:port", 
    io_loop=tornado.ioloop.IOLoop.instance()
)     

The solution came from this ticket: https://github.com/heynemann/motorengine/issues/82.


PS: For Sanic >= 0.4.0 you should consider using sanic-motor extension!

like image 184
John Moutafis Avatar answered Dec 05 '25 11:12

John Moutafis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!