我已经安装了django easy-thumbanils 2.2。到目前为止它一直运行良好,但我决定使其异步以提高性能。在docs之后,我实现了以下代码:
models.py
#saved_file.connect(generate_aliases_global) -> Code used for synchronous operation
@receiver(saved_file)
def generate_thumbnails_async(sender, fieldfile, **kwargs):
print('Calling') #This is being called during an upload
tasks.add.delay(10,2) # A Test function in tasks.py. Confirmed it is being called and the result outputted by the worker
tasks.generate_thumbnails.delay(
model=sender, pk=fieldfile.instance.pk,
field=fieldfile.field.name) #this function not called????
tasks.py
from easy_thumbnails.files import generate_all_aliases
@task
def generate_thumbnails(model, pk, field):
print('Calling gen_thumb task') #not called from models.py
instance = model._default_manager.get(pk=pk)
fieldfile = getattr(instance, field)
generate_all_aliases(fieldfile, include_global=True)
@shared_task
def add(x, y):
print('Calling ad task') #Called from models.py
return x + y
为什么要调用add任务而不是generate_thumbnails任务呢?