I am working with django as a framework for my web application.
For using celery I have installed django-celery, celery and celer[redis].
When it tried to start the celery worker it shows error
Cannot connect to redis://localhost:6379/0: Error 10061 connecting to localhost:6379. No connection could be made because the target machine actively refused it..
Trying again in 6.00 seconds…
I am using a windows laptop. How can I start the redis://localhost:6379/0 sever.
This is the result of running the worker
$ celery worker -A myemail.celery -l info
-------------- celery@LAPTOP-ERVJPN6C v4.3.0 (rhubarb)
---- **** -----
--- * *** * -- Windows-10-10.0.18362-SP0 2019-12-30 19:35:13
-- * - **** ---
- ** ---------- [config]
- ** ---------- .> app: myemail:0x38d56d0
- ** ---------- .> transport: redis://localhost:6379/0
- ** ---------- .> results:
- *** --- * --- .> concurrency: 8 (prefork)
-- ******* ---- .> task events: OFF (enable -E to monitor tasks in this worker)
--- ***** -----
-------------- [queues]
.> celery exchange=celery(direct) key=celery
[tasks]
. email.tasks.send_confirmation_email_to_subscriber
[2019-12-30 19:35:14,763: INFO/SpawnPoolWorker-1] child process 17836 calling self.run()
[2019-12-30 19:35:14,782: INFO/SpawnPoolWorker-4] child process 19512 calling self.run()
[2019-12-30 19:35:14,782: INFO/SpawnPoolWorker-6] child process 6816 calling self.run()
[2019-12-30 19:35:14,789: INFO/SpawnPoolWorker-8] child process 12316 calling self.run()
[2019-12-30 19:35:14,793: INFO/SpawnPoolWorker-2] child process 15580 calling self.run()
[2019-12-30 19:35:14,799: INFO/SpawnPoolWorker-5] child process 18588 calling self.run()
[2019-12-30 19:35:14,801: INFO/SpawnPoolWorker-3] child process 14108 calling self.run()
[2019-12-30 19:35:14,802: INFO/SpawnPoolWorker-7] child process 8944 calling self.run()
[2019-12-30 19:35:18,230: ERROR/MainProcess] consumer: Cannot connect to redis://localhost:6379/0: Error 10061 connecting to localhost:6379. No connection could be made because the target machine actively refused it..
Trying again in 2.00 seconds...
[2019-12-30 19:35:24,252: ERROR/MainProcess] consumer: Cannot connect to redis://localhost:6379/0: Error 10061 connecting to localhost:6379. No connection could be made because the target machine actively refused it..
Trying again in 4.00 seconds...
[2019-12-30 19:35:32,289: ERROR/MainProcess] consumer: Cannot connect to redis://localhost:6379/0: Error 10061 connecting to localhost:6379. No connection could be made because the target machine actively refused it..
Trying again in 6.00 seconds...
settings file
import os
from dotenv import load_dotenv
# // Setting up env variables \
project_folder = os.path.expanduser("~ProjectsworkbenchPythonPython workbenchsrc")
load_dotenv(os.path.join(project_folder, '.env'))
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = os.getenv("SECRET_KEY")
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = [
'localhost',
]
# Application definition
INSTALLED_APPS = [
'myemailapp',
'user',
'crispy_forms',
'markdownify',
'django_celery_results',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
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',
]
ROOT_URLCONF = 'myemail.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(BASE_DIR, 'templates'),
],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'myemail.wsgi.application'
# Database
# https://docs.djangoproject.com/en/2.2/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Password validation
# https://docs.djangoproject.com/en/2.2/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/2.2/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.2/howto/static-files/
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static')
]
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
CRISPY_TEMPLATE_PACK = 'bootstrap4'
# // User acctount credentials \
LOGIN_URL = 'user:login'
LOGIN_REDIRECT_URL = 'myemailapp:myemailapp'
LOGOUT_REDIRECT_URL = 'user:login'
# // email-configs \
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = ''
EMAIL_HOST_PASSWORD = ''
MAILING_LIST_FORM_EMAIL = "[email protected]"
MAILING_LIST_LINK_DOMAIN = "http://localhost:8000"
# // Celery configurations \
CELERY_BROKER_URL = 'redis://localhost:6379/0'
CELERY_RESULT_BACKEND = 'django-db'
celery.py file
import os
from celery import Celery
# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myemail.settings')
app = Celery('myemail')
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks()
2
Answers
I solved this problem. You can download the redis zip file containing the server and client exe which will run on windows server. You can download the file from here
1. Just unzip the file and Run the redis-server.exe file.
2. Then run the redis-cli.exe type ping and it will return PONG. This shows that all are working well
This much is needed.
Tried to run the worker again
For Celery, Django and Heroku, i’ve the same problem, solved with:
It’s a good practice to set your redis URL in settings.py with OS
os.environ.get('REDIS_URL')
Heroku rotates credentials periodically and updates applications where this datastore is attached.
Heroku recommends using encryption and thus a
rediss://
URL instead ofredis://
For celery, something like: CELERY_BROKER_URL =
'rediss://:{Password}@{Host}:{Port}'
Heroku command to get this url easy
heroku redis:credentials REDIS_URL
Referer: https://devcenter.heroku.com/articles/heroku-redis#using-the-cli