I have a web application in Django (4.0.4), Python 3.8 which has a GUI part and a backend tasks part implemented using Celery (5.2.6).
I use lists backed by database tables using stored querysets whose cache I clear before every new access to them using queryset.all()
self._query_set = self._query_set.all()
self._df = pd.DataFrame.from_records(self._query_set)
This works perfetcly in the web part but it SOMETIMES does not work in the backend CELERY part. Calling the .all()
simply does not clear the queryset’s result cache resulting in not reflecting chenges made in the web part. Am I missing something substantial?
the code:
LOG.debug("Reloading content of the database list [{}].".format(self.get_list_names()))
LOG.debug("Querysets count before reload: {}".format(len(self._query_set)))
self._query_set = self._query_set.all()
self._df = pd.DataFrame.from_records(self._query_set)
LOG.debug("Querysets count after reload: {}".format(len(self._query_set)))
Celery backend running since 21.7.2022
Starting CELERY WORKER
-------------- celery@8fcde8905395 v5.2.6 (dawn-chorus)
--- ***** -----
-- ******* ---- Linux-5.4.0-122-generic-x86_64-with-glibc2.2.5 2022-07-21 15:37:46
New records added in the web app 10.8.2022 11:32
[2022-08-10 11:27:49,138]|INFO|vavrova|middleware:log:81|POST|POST /vichr/admin/uzivatele/uzivatel/add/|...ADD USER
[2022-08-10 11:28:57,123]|INFO|vavrova|middleware:log:81|POST|POST /vichr/admin/uzivatele/uzivatel/add/|...ADD USER
[2022-08-10 11:31:01,172]|INFO|vavrova|middleware:log:81|POST|POST /vichr/admin/uzivatele/uzivatel/add/|...ADD USER
[2022-08-10 11:32:08,113]|INFO|vavrova|middleware:log:81|POST|POST /vichr/admin/uzivatele/uzivatel/add/|...ADD USER
backend task run in Celery at 10.8.2022 19:00 - the list was NOT correctly updated
[2022-08-10 19:00:27,924]|DEBUG|[Resources.ListsResource:__enter__:36]|Reloading lists ['seznam_uzivatele_osobni_asistence'] from database (...LIST OF USERS)
[2022-08-10 19:00:27,924]|DEBUG|[Resources.DatabaseListsResource:_load_list_from_database:40]|Reloading content of the database list [['seznam_uzivatele_osobni_asistence']].
[2022-08-10 19:00:27,924]|DEBUG|[Resources.DatabaseListsResource:_load_list_from_database:41]|Querysets count before reload: 73
[2022-08-10 19:00:27,941]|DEBUG|[Resources.DatabaseListsResource:_load_list_from_database:44]|Querysets count after reload: 73 (... number of users BEFORE adding new ones - WRONG ...)
Manual restart of the Celery backend at 10.8.2022 22:29:
Starting CELERY WORKER
-------------- celery@8fcde8905395 v5.2.6 (dawn-chorus)
--- ***** -----
-- ******* ---- Linux-5.4.0-124-generic-x86_64-with-glibc2.2.5 2022-08-10 22:29:05
consecutive task at 22:31 CORRECTLY read the contents of the list
[2022-08-10 22:31:38,976]|INFO|[UI.tasks:task_update_forms:85]|TASK started.
[2022-08-10 22:31:39,108]|DEBUG|[Resources.ListsResource:__enter__:36]|Reloading lists ['seznam_uzivatele_osobni_asistence'] from database (...LIST OF USERS)
[2022-08-10 22:31:39,109]|DEBUG|[Resources.DatabaseListsResource:_load_list_from_database:40]|Reloading content of the database list [['seznam_uzivatele_osobni_asistence']].
[2022-08-10 22:31:39,116]|DEBUG|[Resources.DatabaseListsResource:_load_list_from_database:41]|Querysets count before reload: 77 (... number of users AFTER adding new ones - CORRECT ...)
[2022-08-10 22:31:39,127]|DEBUG|[Resources.DatabaseListsResource:_load_list_from_database:44]|Querysets count after reload: 77
I’ve run out of ideas what to try next - what am I doing wrong.
Thanks for any hint.