Skip to content

Instantly share code, notes, and snippets.

Leonardo Antunes antunesleo

View GitHub Profile
@antunesleo
antunesleo / merge-sort.py
Created Apr 25, 2020
Merge sort in python
View merge-sort.py
def merge_sort(ar, start, end):
qtd = end - start
if qtd > 1:
mid = int((start + end) / 2)
merge_sort(ar, start, mid)
merge_sort(ar, mid, end)
intercalate(ar, start, mid, end)
return ar
def intercalate(ar, start, mid, end):
View celery-app-for-docker.py
app = Celery('first_app', broker='redis://redis:6379/2')
View usage-async-code.py
validate_user(user) # First validate user, then jump to next line
save_db_user(user) # First save_db_user, them jump to next line
start_to_send_confirmation_email(user) # We don't want to wait the email be sent to jump to next line, here we should develop some async stuff
return user
View routing-tasks.py
app = Celery('first_app', broker='redis://localhost/2')
# New code below
app.conf.task_routes = {
'celery_stuff.tasks.serve_a_beer': {'queue': 'beer'},
'celery_stuff.tasks.serve_a_coffee': {'queue': 'coffee'}
}
View first_app_with_serve_a_coffee.py
from celery_stuff.tasks import serve_a_beer, serve_a_coffee # Importing the task
def start_serve_a_beer():
""" Starts the execution of a celery task with the delay method.
the delay method doesn't wait the task execution be finished.
"""
serve_a_beer.apply_async(('weiss', '500ml'))
print('This will be executed before the serve_a_beer task be finished')
View serve_a_coffee.py
@app.task
def serve_a_coffee(_type, size):
"""
This is a celery task. Just a normal function with task decorator.
Note that we use the decorator from a celery insance.
"""
print('Serving a {} {} coffee!'.format(size, _type))
sleep(1)
print("""
---------------------------------
View nginx-upstream-example
# With this, when you proxy_pass to all-apis, nginx automically will balance the requests between the servers.
upstream all-apis {
server your-ip-here:5000;
server another-ip-here:5000;
server a-third-ip-here:5000;
}
server {
View celery-serve-a-beer-logs
[2019-11-19 20:24:32,005: WARNING/ForkPoolWorker-1] Serving 500ml of weiss beer!
[2019-11-19 20:24:35,008: WARNING/ForkPoolWorker-1] ------------------------------------------------
. * .. . * *
* * @()Ooc()* o .
(Q@*0CG*O() ___
|\_________/|/ _ | | | | | / | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
View tasks.py
from time import sleep
from celery import Celery
# Creating a celery instance with redis as message broker.
app = Celery('first_app', broker='redis://localhost/2')
@app.task
def serve_a_beer(_type, size):
"""
View first_app.py
from celery_stuff.tasks import serve_a_beer # Importing the task
def start_serve_a_beer():
""" Adds serve_a_beer task to the queue with apply_async method.
the method doesn't wait the task execution be finished.
"""
serve_a_beer.apply_async(('weiss', '500ml')) # Just add's to a queue, to be executed when celery reads the queue
print('This will be executed before the serve_a_beer task be finished')
You can’t perform that action at this time.