Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ]# yum install MariaDB-devel MariaDB-shared
- ]# source /home/myvenv/bin/activate
- ]# pip install mysqlclient
- ]# mysql -u root
- > CREATE DATABASE cloud CHARACTER SET UTF8;
- > CREATE USER cloud@localhost IDENTIFIED BY '1324';
- > CREATE USER cloud@127.0.0.1 IDENTIFIED BY '1324';
- > GRANT ALL PRIVILEGES ON cloud.* to cloud@localhost;
- > GRANT ALL PRIVILEGES ON cloud.* to cloud@127.0.0.1;
- > FLUSH PRIVILEGES;
- ]# cd /home/uwsgi/cloud
- ]# vi cloud/settings.py
- DATABASES = {
- 'default': {
- 'ENGINE': 'django.db.backends.mysql',
- 'NAME': 'cloud',
- 'USER': 'cloud',
- 'PASSWORD': '1324',
- 'HOST': 'localhost',
- 'PORT': '3306',
- 'OPTIONS': {
- 'init_command': "SET sql_mode='STRICT_TRANS_TABLES'",
- },
- }
- }
- ]# ./manage.py migrate
- ]# yum install rabbitmq-server
- ]# rabbitmq-plugins enable rabbitmq_management
- ]# systemctl start rabbitmq-server
- ]# rabbitmqctl add_user cloud 1324
- ]# rabbitmqctl add_vhost cloud
- ]# rabbitmqctl set_user_tags cloud cloud
- ]# rabbitmqctl set_permissions -p cloud cloud ".*" ".*" ".*"
- ]# pip install celery
- ]# pip install django-celery-results
- ]# ./manage.py migrate
- ]# vi cloud/settings.py
- CELERY_BROKER_URL = 'amqp://cloud:1324@localhost:5672/cloud'
- CELERY_RESULT_BACKEND = 'django-db'
- INSTALLED_APPS = [
- 'django.contrib.admin',
- 'django.contrib.auth',
- 'django.contrib.contenttypes',
- 'django.contrib.sessions',
- 'django.contrib.messages',
- 'django.contrib.staticfiles',
- + 'django_celery_results',
- 'channels',
- 'chat',
- ]
- ]# vi cloud/celery.py
- from __future__ import absolute_import
- import os
- from celery import Celery
- app_name = 'cloud'
- os.environ.setdefault('DJANGO_SETTINGS_MODULE', '{}.settings'.format(app_name))
- from django.conf import settings
- app = Celery(app_name)
- app.config_from_object('django.conf:settings', namespace='CELERY')
- app.autodiscover_tasks()
- ]# vi cloud/__init__.py
- from __future__ import absolute_import, unicode_literals
- from .celery import app as celery_app
- __all__ = ['celery_app']
- ]# vi chat/tasks.py
- from __future__ import absolute_import
- from celery import shared_task
- from celery import Task
- from websocket import create_connection
- class ChatTask(Task):
- def on_success(self, retval, task_id, targs, tkwargs):
- msg = '{} success'.format(task_id)
- ws = create_connection("ws://localhost:8001/chat/foo/")
- ws.send(msg)
- ws.close()
- print(msg)
- super(ChatTask, self).on_success(retval, task_id, targs, tkwargs)
- def on_failure(self, exc, task_id, targs, tkwargs, einfo):
- print('{} error'.format(task_id))
- super(ChatTask, self).on_failure(exc, task_id, targs, tkwargs, einfo)
- @shared_task(base=ChatTask)
- def add(x, y):
- return x + y
- @shared_task
- def mul(x, y):
- return x * y
- @shared_task
- def xsum(numbers):
- return sum(numbers)
- ]# celery -A cloud worker -l info
- ]# ./manage.py shell
- >>> from chat.tasks import *
- >>> add.delay(1,1)
- <AsyncResult: 305be35d-1aa8-4c77-996b-ecb86bfc07a1>
- >>> add.apply_async((2,2))
- <AsyncResult: f451a45e-1165-4246-a10d-6fbab28c0892>
Add Comment
Please, Sign In to add comment