lazy_instance.init missing positional parameter argument" 'db"
pwalimbe opened this issue · 2 comments
I have defined and initialized the lazy_mongo attributes as defined. Also I have added routes & models in a separate file which I invoke after server start. I did debug and the before server start steps were properly executed and motor db client with the db connections are opened fine. But it still gives me with the following error:
Process Process-4:
Traceback (most recent call last):
File "/usr/local/Cellar/python/3.7.5/Frameworks/Python.framework/Versions/3.7/lib/python3.7/multiprocessing/process.py", line 297, in _bootstrap
self.run()
File "/usr/local/Cellar/python/3.7.5/Frameworks/Python.framework/Versions/3.7/lib/python3.7/multiprocessing/process.py", line 99, in run
self._target(*self._args, **self._kwargs)
File "/Users/Prasanna/Environments/covcrm/lib/python3.7/site-packages/sanic/server.py", line 892, in serve
trigger_events(before_start, loop)
File "/Users/Prasanna/Environments/covcrm/lib/python3.7/site-packages/sanic/server.py", line 668, in trigger_events
loop.run_until_complete(result)
File "uvloop/loop.pyx", line 1456, in uvloop.loop.Loop.run_until_complete
File "/Users/Prasanna/Environments/covcrm/lib/python3.7/site-packages/sanic_mongodb_ext/__init__.py", line 35, in mongodb_configure
lazy_instance.init(motor_database_client)
TypeError: init() missing 1 required positional argument: 'db'
/usr/local/Cellar/python/3.7.5/Frameworks/Python.framework/Versions/3.7/lib/python3.7/multiprocessing/process.py:313: RuntimeWarning: coroutine 'Loop.create_server' was never awaited
traceback.print_exc()
The relevant code in app file is:
MongoDbExtension(app)
@app.listener("after-server-start")
def add_routes(app):
from blueprints import bp_v1
app.blueprint(bp_v1)
blueprint.py
from models import Artist
@bp_v1.route("/add_artist")
async def handle(request):
artist = Artist(name="A new rockstar!")
await artist.commit()
return json(artist.dump())
models.py:
from umongo import Instance, Document
from umongo.fields import StringField
from app import app
@app.lazy_mongodb.register()
class Artist(Document):
name = StringField(required=True, allow_none=False)
BTW - I noticed that after the 4 connections were opened, it automatically tried to close them and then error happened.
Hello,
Have you read the documentation how to organize Sanic projects properly and checked examples? There are multiple examples that you can easily reuse as would like:
- Sanic documentation - Blueprints
- Sanic GitHub repo with examples
- Examples with this extension (how I did it without any blueprints and it works fine):
Also the following snippet from the original message:
@app.listener("after-server-start")
def add_routes(app):
from blueprints import bp_v1
app.blueprint(bp_v1)
is a bad approach imho. First of all because the intention of having listeners / hooks in app is to run init / deinit certain parts of the application that heavily using connections, db, etc., but not injecting blueprints with routes and views in runtime
For this snippet
from umongo import Instance, Document
from umongo.fields import StringField
from app import app
@app.lazy_mongodb.register()
class Artist(Document):
name = StringField(required=True, allow_none=False)
- It's not necessary to call decorators for lazy instances of uMongo. Check the original documentation for getting a knowledge how to work with uMongo ODM.
- In structured application you need to use direct call to the LAZY_UMONGO attribute, and then call in the certain module for avoiding issues around imports. Example
Actually I did and the only reason I had to put import blueprints into a listener was that it would not compile without it since app import in models was giving a weird error. Anyways, will try others but going from your example to the other one is tedious for a new comer.