django-extra-settings icon indicating copy to clipboard operation
django-extra-settings copied to clipboard

get error in asgi application

Open yswtrue opened this issue 2 years ago • 4 comments
trafficstars

Python version 3.11.5

Django version 4.2.5

Package version 0.10.0

Current behavior (bug description) I created django asgi project by https://github.com/cookiecutter/cookiecutter-django, and add this package, and then start the local.yaml docker-compose file, get these error.

# config/settings/base.py

THIRD_PARTY_APPS = [
    # ...
    "extra_settings",
]



EXTRA_SETTINGS_DEFAULTS = [
    {
        "name": "FIRST_OPEN_IMAGE",
        "type": "image",
        "value": "example.jpg",
    },
]

Container started
PostgreSQL is available
Operations to perform:
  Apply all migrations: admin, auth, authtoken, contenttypes, django_celery_beat, extra_settings, inventories, knox, official, orders, sessions, sites, social_django, taggit, users
Running migrations:
  No migrations to apply.
  Your models in app(s): 'inventories' have changes that are not yet reflected in a migration, and so won't be applied.
  Run 'manage.py makemigrations' to make new migrations, and then re-run 'manage.py migrate' to apply them.
INFO:     Will watch for changes in these directories: ['/app']
INFO:     Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)
INFO:     Started reloader process [7] using WatchFiles
Process SpawnProcess-1:
Traceback (most recent call last):
  File "/usr/local/lib/python3.11/multiprocessing/process.py", line 314, in _bootstrap
    self.run()
  File "/usr/local/lib/python3.11/multiprocessing/process.py", line 108, in run
    self._target(*self._args, **self._kwargs)
  File "/usr/local/lib/python3.11/site-packages/uvicorn/_subprocess.py", line 76, in subprocess_started
    target(sockets=sockets)
  File "/usr/local/lib/python3.11/site-packages/uvicorn/server.py", line 61, in run
    return asyncio.run(self.serve(sockets=sockets))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/asyncio/runners.py", line 190, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/asyncio/runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "uvloop/loop.pyx", line 1517, in uvloop.loop.Loop.run_until_complete
  File "/usr/local/lib/python3.11/site-packages/uvicorn/server.py", line 68, in serve
    config.load()
  File "/usr/local/lib/python3.11/site-packages/uvicorn/config.py", line 467, in load
    self.loaded_app = import_from_string(self.app)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/uvicorn/importer.py", line 21, in import_from_string
    module = importlib.import_module(module_str)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1204, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 940, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/app/config/asgi.py", line 25, in <module>
    django_application = get_asgi_application()
                         ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/django/core/asgi.py", line 12, in get_asgi_application
    django.setup(set_prefix=False)
  File "/usr/local/lib/python3.11/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/usr/local/lib/python3.11/site-packages/django/apps/registry.py", line 124, in populate
    app_config.ready()
  File "/usr/local/lib/python3.11/site-packages/extra_settings/apps.py", line 17, in ready
    Setting.set_defaults_from_settings()
  File "/usr/local/lib/python3.11/site-packages/extra_settings/models.py", line 100, in set_defaults_from_settings
    cls.set_defaults(settings.EXTRA_SETTINGS_DEFAULTS)
  File "/usr/local/lib/python3.11/site-packages/extra_settings/models.py", line 86, in set_defaults
    setting_obj, setting_created = cls.objects.get_or_create(
                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/django/db/models/manager.py", line 87, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/django/db/models/query.py", line 916, in get_or_create
    return self.get(**kwargs), False
           ^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/django/db/models/query.py", line 633, in get
    num = len(clone)
          ^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/django/db/models/query.py", line 380, in __len__
    self._fetch_all()
  File "/usr/local/lib/python3.11/site-packages/django/db/models/query.py", line 1881, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/django/db/models/query.py", line 91, in __iter__
    results = compiler.execute_sql(
              ^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/django/db/models/sql/compiler.py", line 1560, in execute_sql
    cursor = self.connection.cursor()
             ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/django/utils/asyncio.py", line 24, in inner
    raise SynchronousOnlyOperation(message)
django.core.exceptions.SynchronousOnlyOperation: You cannot call this from an async context - use a thread or sync_to_async.

Expected behavior

I think maybe need to check current runtime is sync or async and use sync_to_async decoration

Upvote & Fund

  • We're using Polar.sh so you can upvote and help fund this issue.
  • We receive the funding once the issue is completed & confirmed by you.
  • Thank you in advance for helping prioritize & fund our backlog.
Fund with Polar

yswtrue avatar Sep 26 '23 19:09 yswtrue

@yswtrue thank you for reporting this, could you submit a PR?

fabiocaccamo avatar Sep 27 '23 14:09 fabiocaccamo

I am trying on it

yswtrue avatar Sep 28 '23 04:09 yswtrue

@yswtrue have you add the possibility to debug/fix this?

fabiocaccamo avatar Nov 01 '23 10:11 fabiocaccamo

I have tried, but I don't know how to debug it.

yswtrue avatar Nov 01 '23 11:11 yswtrue

@yswtrue could you please try to upgrade to 0.12.0 version and let me know?

fabiocaccamo avatar Feb 27 '24 11:02 fabiocaccamo

@yswtrue I'll re-open it in case you will provide some feedback.

fabiocaccamo avatar Mar 04 '24 20:03 fabiocaccamo