mirror of
https://github.com/pretix/pretix.git
synced 2026-05-07 15:34:02 +00:00
Add profiling for celery tasks
This commit is contained in:
@@ -7,11 +7,38 @@ Usage:
|
||||
@task(base=TransactionAwareTask)
|
||||
def task_…():
|
||||
"""
|
||||
from celery import Task
|
||||
import cProfile
|
||||
import os
|
||||
import random
|
||||
import time
|
||||
|
||||
from django.conf import settings
|
||||
from django.db import transaction
|
||||
|
||||
from pretix.celery import app
|
||||
|
||||
class TransactionAwareTask(Task):
|
||||
|
||||
class ProfiledTask(app.Task):
|
||||
abstract = True
|
||||
|
||||
def __call__(self, *args, **kwargs):
|
||||
|
||||
if settings.PROFILING_RATE > 0 and random.random() < settings.PROFILING_RATE / 100:
|
||||
profiler = cProfile.Profile()
|
||||
profiler.enable()
|
||||
starttime = time.time()
|
||||
ret = super().__call__(*args, **kwargs)
|
||||
profiler.disable()
|
||||
tottime = time.time() - starttime
|
||||
profiler.dump_stats(os.path.join(settings.PROFILE_DIR, '{time:.0f}_{tottime:.3f}_celery_{t}.pstat'.format(
|
||||
t=self.name, tottime=tottime, time=time.time()
|
||||
)))
|
||||
return ret
|
||||
else:
|
||||
return super().__call__(*args, **kwargs)
|
||||
|
||||
|
||||
class TransactionAwareTask(ProfiledTask):
|
||||
"""
|
||||
Task class which is aware of django db transactions and only executes tasks
|
||||
after transaction has been committed
|
||||
|
||||
Reference in New Issue
Block a user