connection refused with Celery

2024/9/8 10:42:20

I have a Django project on an Ubuntu EC2 node, which I have been using to set up an asynchronous using Celery.

I am following How to list the queued items in celery? along with the docs, to experiment with celery at the command line.

I've been able to get a basic task working at the command line, using:

(env1)ubuntu@ip-172-31-22-65:~/projects/tp$ celery --app=myproject.celery:app worker --loglevel=INFO

However, if I run other celery commands like below I'm getting the following:

(env1)ubuntu@ip-172-31-22-65:~/projects/tp$ celery inspect ping
Traceback (most recent call last):File "/home/ubuntu/.virtualenvs/env1/bin/celery", line 11, in <module>sys.exit(main())File "/home/ubuntu/.virtualenvs/env1/lib/python3.4/site-packages/celery/__main__.py", line 30, in mainmain()File "/home/ubuntu/.virtualenvs/env1/lib/python3.4/site-packages/celery/bin/celery.py", line 81, in maincmd.execute_from_commandline(argv)File "/home/ubuntu/.virtualenvs/env1/lib/python3.4/site-packages/celery/bin/celery.py", line 769, in execute_from_commandlinesuper(CeleryCommand, self).execute_from_commandline(argv)))File "/home/ubuntu/.virtualenvs/env1/lib/python3.4/site-packages/celery/bin/base.py", line 307, in execute_from_commandlinereturn self.handle_argv(self.prog_name, argv[1:])File "/home/ubuntu/.virtualenvs/env1/lib/python3.4/site-packages/celery/bin/celery.py", line 761, in handle_argvreturn self.execute(command, argv)File "/home/ubuntu/.virtualenvs/env1/lib/python3.4/site-packages/celery/bin/celery.py", line 693, in execute).run_from_argv(self.prog_name, argv[1:], command=argv[0])File "/home/ubuntu/.virtualenvs/env1/lib/python3.4/site-packages/celery/bin/base.py", line 311, in run_from_argvsys.argv if argv is None else argv, command)File "/home/ubuntu/.virtualenvs/env1/lib/python3.4/site-packages/celery/bin/base.py", line 373, in handle_argvreturn self(*args, **options)File "/home/ubuntu/.virtualenvs/env1/lib/python3.4/site-packages/celery/bin/base.py", line 270, in __call__ret = self.run(*args, **kwargs)File "/home/ubuntu/.virtualenvs/env1/lib/python3.4/site-packages/celery/bin/celery.py", line 324, in runreturn self.do_call_method(args, **kwargs)File "/home/ubuntu/.virtualenvs/env1/lib/python3.4/site-packages/celery/bin/celery.py", line 346, in do_call_methodcallback=self.say_remote_command_reply)File "/home/ubuntu/.virtualenvs/env1/lib/python3.4/site-packages/celery/bin/celery.py", line 385, in callreturn getattr(i, method)(*args)File "/home/ubuntu/.virtualenvs/env1/lib/python3.4/site-packages/celery/app/control.py", line 100, in pingreturn self._request('ping')File "/home/ubuntu/.virtualenvs/env1/lib/python3.4/site-packages/celery/app/control.py", line 71, in _requesttimeout=self.timeout, reply=True,File "/home/ubuntu/.virtualenvs/env1/lib/python3.4/site-packages/celery/app/control.py", line 307, in broadcastlimit, callback, channel=channel,File "/home/ubuntu/.virtualenvs/env1/lib/python3.4/site-packages/kombu/pidbox.py", line 283, in _broadcastchan = channel or self.connection.default_channelFile "/home/ubuntu/.virtualenvs/env1/lib/python3.4/site-packages/kombu/connection.py", line 756, in default_channelself.connectionFile "/home/ubuntu/.virtualenvs/env1/lib/python3.4/site-packages/kombu/connection.py", line 741, in connectionself._connection = self._establish_connection()File "/home/ubuntu/.virtualenvs/env1/lib/python3.4/site-packages/kombu/connection.py", line 696, in _establish_connectionconn = self.transport.establish_connection()File "/home/ubuntu/.virtualenvs/env1/lib/python3.4/site-packages/kombu/transport/pyamqp.py", line 112, in establish_connectionconn = self.Connection(**opts)File "/home/ubuntu/.virtualenvs/env1/lib/python3.4/site-packages/amqp/connection.py", line 165, in __init__self.transport = self.Transport(host, connect_timeout, ssl)File "/home/ubuntu/.virtualenvs/env1/lib/python3.4/site-packages/amqp/connection.py", line 186, in Transportreturn create_transport(host, connect_timeout, ssl)File "/home/ubuntu/.virtualenvs/env1/lib/python3.4/site-packages/amqp/transport.py", line 299, in create_transportreturn TCPTransport(host, connect_timeout)File "/home/ubuntu/.virtualenvs/env1/lib/python3.4/site-packages/amqp/transport.py", line 95, in __init__raise socket.error(last_err)
OSError: [Errno 111] Connection refused

The installed python packages:

(env1)ubuntu@ip-172-31-22-65:~/projects/tp$ pip freeze
amqp==1.4.6
anyjson==0.3.3
billiard==3.3.0.19
celery==3.1.17
Django==1.7.7
django-redis-cache==0.13.0
kombu==3.0.24
pytz==2015.2
redis==2.10.3
requests==2.6.0
uWSGI==2.0.10

/projects/tp/tp/celery.py

from __future__ import absolute_importimport os
import django
from celery import Celery
from django.conf import settings# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'tp.settings')
django.setup()app = Celery('hello_django')# Using a string here means the worker will not have to
# pickle the object when using Windows.
app.config_from_object('django.conf:settings')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)

also , in redis.conf:

# Specify the path for the unix socket that will be used to listen for
# incoming connections. There is no default, so Redis will not listen
# on a unix socket when not specified.
#unixsocket /var/run/redis/redis.sockunixsocketperm 777

tp.settings.py:

# CELERY SETTINGS
BROKER_URL = 'redis://localhost:6379/0'
CELERY_ACCEPT_CONTENT = ['json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'CACHES = {'default': {'BACKEND': 'redis_cache.RedisCache','LOCATION': '/var/run/redis/redis.sock',},
}

edit 2:

ubuntu@ip-172-31-22-65:~$ redis-cli ping
PONG
ubuntu@ip-172-31-22-65:~$ service redis-server status
redis-server is not running

edit 3:

(env1)ubuntu@ip-172-31-22-65:~/projects/tp$ redis-cli ping
PONG
(env1)ubuntu@ip-172-31-22-65:~/projects/tp$ sudo service redis-server start
Starting redis-server: failed
(env1)ubuntu@ip-172-31-22-65:~/projects/tp$ service redis-server status
redis-server is not running

What am I doing wrong?

Answer

Try this add it to project/__init__.py it should work, app is always imported so shared_task can use this app

from __future__ import absolute_import
from .celery import app as celery_app__all__ = ('celery_app',)
https://en.xdnf.cn/q/72681.html

Related Q&A

Routing all packets through my program?

I want to build an application that routes all network traffic (not just HTTP) through my application. Basically, what I want is all the traffic to be given to my application (they should never reach t…

python struct.pack(): pack multiple datas in a list or a tuple

Say i have a list or a tuple containing numbers of type long long,x = [12974658, 638364, 53637, 63738363]If want to struct.pack them individually, i have to use struct.pack(<Q, 12974658)or if i want…

Can I pass a list of colors for points to matplotlibs Axes.plot()?

Ive got a lot of points to plot and am noticing that plotting them individually in matplotlib takes much longer (more than 100 times longer, according to cProfile) than plotting them all at once. Howev…

Tidy data from multilevel Excel file via pandas

I want to produce tidy data from an Excel file which looks like this, with three levels of "merged" headers:Pandas reads the file just fine, with multilevel headers:# df = pandas.read_excel(t…

ttk tkinter multiple frames/windows

The following application I have created is used to demonstrate multiple windows in tkinter. The main problem is that none of the Entry controls, neither in the bmi-calculator or the converter, accept …

input() vs sys.stdin.read()

import sys s1 = input() s2 = sys.stdin.read(1)#type "s" for examples1 == "s" #False s2 == "s" #TrueWhy? How can I make input() to work properly? I tried to encode/decode…

Renormalize weight matrix using TensorFlow

Id like to add a max norm constraint to several of the weight matrices in my TensorFlow graph, ala Torchs renorm method.If the L2 norm of any neurons weight matrix exceeds max_norm, Id like to scale it…

Numpy: find the euclidean distance between two 3-D arrays

Given, two 3-D arrays of dimensions (2,2,2):A = [[[ 0, 0],[92, 92]],[[ 0, 92],[ 0, 92]]]B = [[[ 0, 0],[92, 0]],[[ 0, 92],[92, 92]]]How do you find the Euclidean distance for each vector in A and B e…

Is it possible to break from lambda when the expected result is found

I am Python newbie, and just become very interested in Lambda expression. The problem I have is to find one and only one target element from a list of elements with lambda filter. In theory, when the t…

Intersection of multiple pandas dataframes

I have a number of dataframes (100) in a list as:frameList = [df1,df2,..,df100]Each dataframe has the two columns DateTime, Temperature.I want to intersect all the dataframes on the common DateTime col…