├── doctor.txt ├── .gitignore ├── img ├── rabbitmq.png ├── celery-process.png └── starting-celery.png ├── client.py ├── requirements.txt ├── celeryconfig.py ├── tasks.py └── README.md /doctor.txt: -------------------------------------------------------------------------------- 1 | 1 -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.pyc 2 | env 3 | -------------------------------------------------------------------------------- /img/rabbitmq.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DevelopHerDevelopHim/celery-task/HEAD/img/rabbitmq.png -------------------------------------------------------------------------------- /client.py: -------------------------------------------------------------------------------- 1 | from celery import Celery 2 | 3 | celery = Celery() 4 | celery.config_from_object('celeryconfig') 5 | -------------------------------------------------------------------------------- /img/celery-process.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DevelopHerDevelopHim/celery-task/HEAD/img/celery-process.png -------------------------------------------------------------------------------- /img/starting-celery.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DevelopHerDevelopHim/celery-task/HEAD/img/starting-celery.png -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | amqp==1.4.7 2 | anyjson==0.3.3 3 | billiard==3.3.0.21 4 | celery==3.1.19 5 | Flask==0.10.1 6 | itsdangerous==0.24 7 | Jinja2==2.8 8 | kombu==3.0.29 9 | MarkupSafe==0.23 10 | pytz==2015.7 11 | Werkzeug==0.11.2 12 | wheel==0.24.0 13 | -------------------------------------------------------------------------------- /celeryconfig.py: -------------------------------------------------------------------------------- 1 | CELERY_IMPORTS = ('tasks') 2 | CELERY_IGNORE_RESULT = False 3 | BROKER_HOST = "127.0.0.1" #IP address of the server running RabbitMQ and Celery 4 | BROKER_PORT = 5672 5 | BROKER_URL = 'amqp://' 6 | 7 | from datetime import timedelta 8 | 9 | CELERYBEAT_SCHEDULE = { 10 | 'doctor-every-10-seconds': { 11 | 'task': 'tasks.fav_doctor', 12 | 'schedule': timedelta(seconds=10), 13 | }, 14 | } 15 | -------------------------------------------------------------------------------- /tasks.py: -------------------------------------------------------------------------------- 1 | from celery.task import task 2 | 3 | suf = lambda n: "%d%s" % (n, {1: "st", 2: "nd", 3: "rd"}.get(n if n < 20 else n % 10, "th")) 4 | 5 | 6 | @task 7 | def fav_doctor(): 8 | """Reads doctor.txt file and prints out fav doctor, then adds a new 9 | number to the file""" 10 | 11 | with open('doctor.txt', 'r+') as f: 12 | for line in f: 13 | nums = line.rstrip().split() 14 | 15 | print 'The {} doctor is my favorite'.format(suf(int(nums[0]))) 16 | 17 | for num in nums[1:]: 18 | print 'Wait! The {} doctor is my favorite'.format(suf(int(num))) 19 | 20 | last_num = int(nums[-1]) 21 | new_last_num = last_num + 1 22 | 23 | f.write(str(new_last_num) + ' ') 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Simple Celery - Doctor Who 2 | -------------------------- 3 | 4 | 5 | **Description** 6 | 7 | A simple celery task using the heartbeat feature in Python. 8 | 9 | Reads a file that prints to the console and writes to the file every 10 seconds. 10 | 11 | For full details on the process of creating this project: [Video](https://www.youtube.com/watch?v=waD4MEj8WGw), [Blog](https://chatasweetie.com/2016/09/02/celery-distributed-task-queue/) 12 | 13 | **How it works** 14 | 15 | The Celery worker (celery is an asynchronous task queue/job queue based on distributed message passing) opens the text file and iterates over the file, printing to the console. At the end of the file, it addes to the file another iteration and closes the file. This repeats every 10 seconds. 16 | 17 | 18 | ### Screenshot 19 | 20 | **Starting RabbitMQ** 21 | 22 | 23 | 24 | **Starting Celery Worker** 25 | 26 | 27 | 28 | **Celery Worker** 29 | 30 | 31 | 32 | 33 | ### Technology Stack 34 | 35 | **Application:** Python, Celery, RabbitMQ 36 | 37 | 38 | ### How to run locally 39 | 40 | Download [RabbitMQ](https://www.rabbitmq.com/) 41 | 42 | 43 | Create a virtual environment 44 | 45 | ``` 46 | >>> virtualenv env 47 | >>> source env/bin/activate 48 | ``` 49 | 50 | Install the dependencies 51 | 52 | ``` 53 | >>> pip install -r requirements.txt 54 | ``` 55 | 56 | Run RabbitMQ server 57 | 58 | ``` 59 | >>> cd rabbitmq_server-3.5.6/ 60 | >>> sbin/rabbitmq-server 61 | ``` 62 | 63 | In a new terminal run Celery worker 64 | ``` 65 | >>> celery worker -l info --beat 66 | ``` 67 | 68 | 69 | ### About the Developer 70 | Jessica Dene Earley 71 | [Short Bio](https://chatasweetie.com/about-me/) 72 | [Linkedin](https://www.linkedin.com/in/jessicaearley) 73 | [Chatasweetie's Blog](https://chatasweetie.com/) --------------------------------------------------------------------------------