django-q2 icon indicating copy to clipboard operation
django-q2 copied to clipboard

async_iter save operation broken

Open danjac opened this issue 10 months ago • 0 comments

Python 3.13 Django 5.2 django-q2 1.7.6

Using async_iter the tasks are run correctly, however it appears the save operation at the end of the run is broken.


def run_tasks((*, limit: int = 360) -> None:
    logger.info("Parsing %s items", limit)
    result_id = async_iter(
        run_task,
        _get_list_of_ids(limit),
    )
    if result_list := result(result_id):
        logger.info("Handled %s items", len(result_list))
    else:
        logger.info("No items")


def run_task(item_id: int) -> str:
    item = Item.objects.get(pk=item_id)
   do_operation_with_item(item)
Traceback (most recent call last):
  File "/path-to-project/.venv/lib64/python3.13/site-packages/django_q/monitor.py", line 123, in save_task
    task_obj = Task.objects.get(id=task["id"], name=task["name"])
  File "/path-to-project/.venv/lib64/python3.13/site-packages/django/db/models/manager.py", line 87, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
  File "/path-to-project/.venv/lib64/python3.13/site-packages/django/db/models/query.py", line 633, in get
    raise self.model.DoesNotExist(
        "%s matching query does not exist." % self.model._meta.object_name
    )
django_q.models.Task.DoesNotExist: Task matching query does not exist.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/path-to-project/.venv/lib64/python3.13/site-packages/django/db/backends/utils.py", line 105, in _execute
    return self.cursor.execute(sql, params)
           ~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^
  File "/path-to-project/.venv/lib64/python3.13/site-packages/psycopg/cursor.py", line 97, in execute
    raise ex.with_traceback(None)
psycopg.errors.CardinalityViolation: more than one row returned by a subquery used as an expression

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/path-to-project/.venv/lib64/python3.13/site-packages/django_q/monitor.py", line 135, in save_task
    task_obj = Task.objects.create(
        id=task["id"],
    ...<11 lines>...
        attempt_count=1,
    )
  File "/path-to-project/.venv/lib64/python3.13/site-packages/django/db/models/manager.py", line 87, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
  File "/path-to-project/.venv/lib64/python3.13/site-packages/django/db/models/query.py", line 663, in create
    obj.save(force_insert=True, using=self.db)
    ~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/path-to-project/.venv/lib64/python3.13/site-packages/django/db/models/base.py", line 902, in save
    self.save_base(
    ~~~~~~~~~~~~~~^
        using=using,
        ^^^^^^^^^^^^
    ...<2 lines>...
        update_fields=update_fields,
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/path-to-project/.venv/lib64/python3.13/site-packages/django/db/models/base.py", line 1008, in save_base
    updated = self._save_table(
        raw,
    ...<4 lines>...
        update_fields,
    )
  File "/path-to-project/.venv/lib64/python3.13/site-packages/django/db/models/base.py", line 1169, in _save_table
    results = self._do_insert(
        cls._base_manager, using, fields, returning_fields, raw
    )
  File "/path-to-project/.venv/lib64/python3.13/site-packages/django/db/models/base.py", line 1210, in _do_insert
    return manager._insert(
           ~~~~~~~~~~~~~~~^
        [self],
        ^^^^^^^
    ...<3 lines>...
        raw=raw,
        ^^^^^^^^
    )
    ^
  File "/path-to-project/.venv/lib64/python3.13/site-packages/django/db/models/manager.py", line 87, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
  File "/path-to-project/.venv/lib64/python3.13/site-packages/django/db/models/query.py", line 1854, in _insert
    return query.get_compiler(using=using).execute_sql(returning_fields)
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^
  File "/path-to-project/.venv/lib64/python3.13/site-packages/django/db/models/sql/compiler.py", line 1881, in execute_sql
    cursor.execute(sql, params)
    ~~~~~~~~~~~~~~^^^^^^^^^^^^^
  File "/path-to-project/.venv/lib64/python3.13/site-packages/django/db/backends/utils.py", line 122, in execute
    return super().execute(sql, params)
           ~~~~~~~~~~~~~~~^^^^^^^^^^^^^
  File "/path-to-project/.venv/lib64/python3.13/site-packages/django/db/backends/utils.py", line 79, in execute
    return self._execute_with_wrappers(
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~^
        sql, params, many=False, executor=self._execute
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/path-to-project/.venv/lib64/python3.13/site-packages/django/db/backends/utils.py", line 92, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/path-to-project/.venv/lib64/python3.13/site-packages/django/db/backends/utils.py", line 100, in _execute
    with self.db.wrap_database_errors:
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/path-to-project/.venv/lib64/python3.13/site-packages/django/db/utils.py", line 91, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/path-to-project/.venv/lib64/python3.13/site-packages/django/db/backends/utils.py", line 105, in _execute
    return self.cursor.execute(sql, params)
           ~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^
  File "/path-to-project/.venv/lib64/python3.13/site-packages/psycopg/cursor.py", line 97, in execute
    raise ex.with_traceback(None)
django.db.utils.ProgrammingError: more than one row returned by a subquery used as an expression

danjac avatar Apr 18 '25 11:04 danjac