├── .gitignore ├── LICENSE ├── README.md ├── docker-compose.yml ├── requirements.txt └── tasks.py /.gitignore: -------------------------------------------------------------------------------- 1 | venv 2 | .idea/ 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2021 Valon Januzaj 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | SOFTWARE. 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # python-asynchronous-tasks 2 | 3 | 4 | ## Setup & Installation 5 | Create a virtual environment and install the dependencies: 6 | ```bash 7 | $ python -m venv venv 8 | $ source env/bin/activate 9 | 10 | $ pip install -r requirements.txt 11 | ``` 12 | 13 | Start redis and rabbitmq services with `docker-compose`: 14 | ```bash 15 | docker-compose up -d 16 | ``` 17 | 18 | Verify that the services are up and running: 19 | ``` 20 | $ docker ps 21 | 22 | # Excepted output 23 | 27d1b8ced89a rabbitmq:latest "docker-entrypoint.s…" 8 minutes ago Up 8 minutes 4369/tcp, 5671/tcp, 15691-15692/tcp, 25672/tcp, 0.0.0.0:5672->5672/tcp, :::5672->5672/tcp python-async-tasks_rabbitmq_1 24 | 17b72947fb8a redis:latest "docker-entrypoint.s…" 8 minutes ago Up 8 minutes 0.0.0.0:6379->6379/tcp, :::6379->6379/tcp python-async-tasks_redis_1 25 | ``` 26 | 27 | ## Usage 28 | Spin up a new terminal and start the celery worker: 29 | ```bash 30 | $ celery -A tasks worker -l info --pool=solo 31 | ``` 32 | Send a task to celery worker to verify that it's working as excepted: 33 | Open a new terminal and get inside python interactive shell, import `say_hello` function from `tasks.py` and then call it with `delay()` 34 | 35 | ```python 36 | $ python 37 | >>> from tasks import say_hello 38 | >>> say_hello.delay("Valon") 39 | 40 | ``` 41 | And in the celery terminal that we started the worker before we should except a result similar to: 42 | ``` 43 | INFO/MainProcess] Received task: tasks.say_hello[5711efdc-17c7-4f02-bfba-d193c3d28c42] 44 | INFO/MainProcess] Task tasks.say_hello[5711efdc-17c7-4f02-bfba-d193c3d28c42] succeeded in 5.01600000000326s: 'Hello Valon' 45 | ``` 46 | 47 | ## What to do next? 48 | Just expand the usage of the celery, define your tasks and keep doing amazing stuff :) 49 | 50 | ## Contact: 51 | If you have any questions regarding the topic or anything else, feel free to reach to me on:
52 | * [LinkedIn](https://www.linkedin.com/in/valon-januzaj-b02692187/)
53 | * [Github](https://github.com/vjanz)
54 | * [Email](mailto:valon.januzaj98@gmail.com) 55 | 56 | 57 | ## License 58 | This project is licensed under the terms of the MIT license. 59 | 60 | 61 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: "3" 2 | services: 3 | rabbitmq: 4 | image: rabbitmq:latest 5 | environment: 6 | - RABBITMQ_DEFAULT_USER=guest 7 | - RABBITMQ_DEFAULT_PASS=guest 8 | ports: 9 | - "5672:5672" 10 | 11 | redis: 12 | image: redis:latest 13 | ports: 14 | - "6379:6379" -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | amqp==5.0.6 2 | billiard==3.6.4.0 3 | celery==5.0.5 4 | click==7.1.2 5 | click-didyoumean==0.0.3 6 | click-plugins==1.1.1 7 | click-repl==0.2.0 8 | kombu==5.1.0 9 | prompt-toolkit==3.0.18 10 | pytz==2021.1 11 | redis==3.5.3 12 | six==1.16.0 13 | vine==5.0.0 14 | wcwidth==0.2.5 15 | -------------------------------------------------------------------------------- /tasks.py: -------------------------------------------------------------------------------- 1 | from celery import Celery 2 | from time import sleep 3 | 4 | broker_url = "amqp://localhost" 5 | redis_url = "redis://localhost" 6 | app = Celery('tasks', broker=broker_url, backend=redis_url) 7 | 8 | 9 | @app.task 10 | def say_hello(name: str): 11 | sleep(5) 12 | return f"Hello {name}" 13 | --------------------------------------------------------------------------------