I want to create a progress bar for my project. I have a class and this class has some functions. Especially, one of them takes a long time (def download_all) and this is my main reason for wanting to create a progress bar.
I successfully set up celery, celery-progress, etc. and they work all fine. My problem is this: I want to integrate the progress bar to download_all function. I
It gives an error: AttributeError: 'download_all' object has no attribute 'location' I am sure that this error is because of the celery task because when I run it without celery, it works.
This is my code. How can I do it? views.py
def setup_wizard(request):
...
task = (functions.myClass(n_username, n_password,
n_url, n_port, db_password,
username=request.user.username)).delay(5)
task_id = task.download_all(request.user.username, setup.nsername, setup.n_password,
setup.n_url, setup.ns_port, setup.db_password,
username=request.user.username).task_id
# in task_id download_all parameters, I had to user request.user.username instead of "self" parameter.
...
context = {
...
'task_id':task_id
}
functions.py
class myClass():
def __init__(self, n_user, n_password, n_url, n_port, db_password, username):
self.location = zt_dosya_url
self.static_fields = {}
self.download_all(n_user, n_password, n_url, n_port, db_password)
self.send(username)
...
@shared_task(bind=True)
def download_all(self, n_user, n_password, n_url, n_port, db_password):
if not os.path.exists(self.location):
os.mkdir(self.location)
else:
shutil.rmtree(self.location, ignore_errors=True)
os.mkdir(self.location)
...
for s in scans:
i = 0
scanner.scan_name = s['name']
...
progress_recorder.set_progress(i + 1, len(scans), f'On iteration {i}')
...
i += 1
traceback
Environment:
Request Method: POST
Request URL: http://127.0.0.1:8000/operasyonmerkezi/konfigurasyon
Django Version: 3.2.7
Python Version: 3.9.6
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'dashboard',
'accounts',
'logs',
'crispy_forms',
'django_apscheduler',
'easy_timezones',
'django_celery_results',
'celery_progress']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback (most recent call last):
File "C:\Users\edeni\Desktop\hawkdragon\myvenv\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\Users\edeni\Desktop\hawkdragon\myvenv\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\edeni\Desktop\hawkdragon\myvenv\lib\site-packages\django\contrib\auth\decorators.py", line 21, in _wrapped_view
return view_func(request, *args, **kwargs)
File "C:\Users\edeni\Desktop\hawkdragon\dashboard\views.py", line 128, in setup_wizard
task = (functions.myClass(setup.n_username, setup.n_password,
File "C:\Users\edeni\Desktop\hawkdragon\dashboard\functions.py", line 43, in __init__
self.download_all(self, n_user, n_password, n_url, n_port, db_password)
File "C:\Users\edeni\Desktop\hawkdragon\myvenv\lib\site-packages\celery\local.py", line 188, in __call__
return self._get_current_object()(*a, **kw)
File "C:\Users\edeni\Desktop\hawkdragon\myvenv\lib\site-packages\celery\app\task.py", line 389, in __call__
return self.run(*args, **kwargs)
File "C:\Users\edeni\Desktop\hawkdragon\dashboard\functions.py", line 147, in download_all
progress_recorder.set_progress(i + 1, len_scans, description="Downloading")
File "C:\Users\edeni\Desktop\hawkdragon\myvenv\lib\site-packages\celery_progress\backend.py", line 46, in set_progress
self.task.update_state(
Exception Type: AttributeError at /operasyonmerkezi/konfigurasyon
Exception Value: 'myClass' object has no attribute 'update_state'