2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

UbuntuのPythonにCeleryをインストール

Last updated at Posted at 2021-09-23

#UbuntuにインストールしたPythonにCeleryをインストールする手順

##外部サーバに接続してワーカーを起動させメッセージを取得する

####開発環境
Ubuntu 20.04.2 LTS
Python 3.8.10
Django 3.2.7
redis 3.5.3


####インストールされているかの確認
ubuntu@tk2-248-33993:/var $ sudo pip freeze
...何かしら色々リストがでる。その中でバージョンが書かれていればOK
celery==5.1.2
Django==3.2.7
redis==3.5.3

####存在しない場合は、インストールすること
pip3 install django
pip3 install celery
pip3 install redis

####varの下にwwwディレクトリを作成する (共通)

ubuntu@tk2-248-33993:/var $ sudo mkdir www

####www の下に django_appディレクトリを作成する (共通)
ubuntu@tk2:/var/www $ sudo mkdir django_app

####django_app に移動 (共通)

ubuntu@tk2:/var/www $ cd django_app

####プロジェクトの作成 (共通)
ubuntu@tk2:/var/www/django_app $ sudo django-admin startproject celery_tutorial

####作成されたcelery_tutorialに移動 (共通)
ubuntu@tk2:/var/www/django_app $ cd celery_tutorial/

####migrateする (共通)
ubuntu@tk2:/var/www/django_app/celery_tutorial $ sudo python3 manage.py migrate

####celery_tutorial に移動する (共通)
ubuntu@tk2:/var/www/django_app/celery_tutorial $ cd celery_tutorial/

####celery.py ファイルを作成 (共通)
ubuntu@tk2:/var/www/django_app/celery_tutorial/celery_tutorial $ sudo vi celery.py

####celery.py に下記を書き込む (共通)
from __future__ import absolute_import, unicode_literals

import os

from celery import Celery

# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'celery_tutorial.settings')

app = Celery('celery_tutorial')

# Using a string here means the worker doesn't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
#   should have a `CELERY_` prefix.
app.config_from_object('django.conf:settings', namespace='CELERY')

# Load task modules from all registered Django app configs.
app.autodiscover_tasks()

####setting.pyを開く(Worker側)
ubuntu@tk2-248-33993:/var/www/django_app/celery_tutorial/celery_tutorial $ sudo vi settings.py

####接続したい先のIPアドレスを追記する(Worker側)
sudo vi /var/www/django_app/celery_tutorial/celery_tutorial/settings.py
CELERY_BROKER_URL = 'redis://1xx.1xx.2xx.2xx:6379'

####接続したい先のIPアドレスを追記する(Broker側)
sudo vi /var/www/django_app/celery_tutorial/celery_tutorial/settings.py
CELERY_BROKER_URL = ['*']

####__init__.pyを開く (共通)
ubuntu@tk2-248-33993:/var/www/django_app/celery_tutorial/celery_tutorial $ sudo vi __init__.py

####__init__.pyに下記を追記 (共通)
from __future__ import absolute_import, unicode_literals

# This will make sure the app is always imported when
# Django starts so that shared_task will use this app.
from .celery import app as celery_app

__all__ = ('celery_app',)

###celery.pyに追加 (Worker側)

from __future__ import absolute_import, unicode_literals

import os

from celery import Celery

# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'celery_tutorial.settings')

app = Celery('celery_tutorial')

# Using a string here means the worker doesn't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
#   should have a `CELERY_` prefix.
app.config_from_object('django.conf:settings', namespace='CELERY')

# Load task modules from all registered Django app configs.
app.autodiscover_tasks()

##ここから追加###
@app.task
def debug_task(x, y):
    print('wwwww')
    return x + y
##追加ここまで###

####/etc/redis/redis.confの変更 外部接続できるようにする(Broker側)
#bind 127.0.0.1 -::1
bind 0.0.0.0

####redis.conf を書替えたら、redisを再起動(Broker側)
ubuntu@tk2-248-33993:/var/www/django_app/celery_tutorial/celery_tutorial $ sudo systemctl status redis
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?