3. Ponto forte do Celery
• Gerenciador de tarefas assíncrono
4. Ponto forte do Celery
• Gerenciador de tarefas
assíncrono
• Distribuído entre máquinas
5. Os dois pontos fortes
do Celery
• Gerenciador de tarefas
assíncrono
• Distribuído entre máquinas
6. Os dois pontos fortes
do Celery
• Gerenciador de tarefas
assíncrono
• Distribuído entre máquinas
• Agendador
7. Celery
Dentre os pontos fortes do Celery
destacam-se
• Gerenciador de tarefas
assíncrono
• Distribuído entre máquinas
• Agendador
• Com tolerância a falhas
28. Problemas comuns
What are the common mistakes you see.
--------------------------------------
Things that take a long time ("sending an email")
that lock up a process. Using the filesystem for caching.
Testing locally and pushing it live and see it fall over.
Using an external API can also take a long time.
Work with timeouts to work around it.
30. Soluções comuns
Tenha um celery de criança:
Scaling wise, what should you think about before scaling
becomes an actual issue? And what should you definitely
leave until the moment comes you need to scale?
----------------------------------------------
First things first: use a queue like celery, even on small sites.
Get used to such a queue and have it in place, that'll help a
lot with performance later.Make sure you've got your database
schema is mostly OK. It doesn't have to be perfect right away,
but at least mostly OK.
Expect something to change and assume you'll have to swap something
out later to improve the performance.
31. email.py
from celery.task import task
from django.core.mail import send_mail
@task
def send(subject, message, recipient_list,
from_email=None, **kwargs):
if subject[0] != "[":
subject = "[projeto] "+ subject
if from_email is None:
from_email = "suporte@fuuuuuu.com"
send_mail(subject, message, from_email,
recipient_list, **kwargs)
32. Um bom padrão
from email import send
@task
def relatorio(dados):
....gera relatorio....
send.delay("Relatorio Pronto",
"""
Pode baixadas daqui:
http://fuuuuuu.com/relatorios/%s.pdf
"""% filename,
(email,))
34. Map Reduce
tarefas = []
livros =["iliada.txt","odisseia.txt"....]
for filename in livros:
tarefas.append(
conta_palavras.delay(filename)
)
para pegar o resultado
if all([i.ready() for i in tarefas]):
palavras = sum([i.result for i in tarefas])
40. Testar
#no settings.py
TEST_RUNNER='djcelery.contrib.test_runner.run_tests'
from django.test import TestCase
from myapp.tasks import add
class AddTestCase(TestCase):
def testNoError(self):
"""Test that the ``add`` task runs
with no errors,
and returns the correct result."""
result = add.delay(8, 8)
self.assertEquals(result.get(), 16)
self.assertTrue(result.successful())
44. Assim
def bar(request):
sanduiche = Sanduiche("Pernil")
sanduiche.save()
if transaction.is_managed():
transaction.commit()
avisa_novo.delay(sanduiche.id)
45. Para desenvolvimento
from socket import error
try:
avisa_novo.delay(sanduiche.id)
except error:
log.error("conexao recusada")
if not DEBUG:
raise error