python - 查看使用 Heroku 和 S3 处理图像花费太多时间

标签 python django heroku amazon-s3

我在 Heroku 中运行一个 Django 应用程序,它处理用户上传的多个图像并将它们存储到 Amazon S3。问题是这个过程的执行通常需要30s以上(Heroku的执行时间限制)。

我测试了一下,耗时较长的那一行是在 ImageField 中保存图像文件的那一行。这样做是因为图像必须由 ProcessImageFile() 裁剪和处理。不过这个函数花的时间并不长,而是save方法本身,可能是因为它在保存文件的同时将文件一个一个地存储在S3中。

这是 View (省略了不相关的行):

    @login_required
    def image_create(request):
        if request.method == 'POST':
            images = request.FILES.getlist("files")
            crop_points = json.loads( request.POST.get('crop_points'))

            #Validation of inputs in the form: images and other fields
            if len(images) < 3 : return JsonResponse({'val_result': 'min_error'})
            if len(images) > 12: return JsonResponse({'val_result': 'max_error'})

            #We Create the gallery, iterate over the images provided by the form, validate, insert custom fields and save them in bulk associating it to the gallery.

            with transaction.atomic():            
                new_items = []    
                gallery = Gallery.objects.create( user=request.user )

                for i, img_file in enumerate(images):
                    new_item = Image()
                    new_item.user = request.user            

                #-----THIS IS THE PART WHICH TAKES MOST OF THE VIEW PROCESSING TIME: IT IS NOT THE ProcessImageFile FUNCTION, BUT THE SAVE METHOD ITSELF
                    new_item.image.save( 'img'+ str(i) + '.jpg', content = ProcessImageFile(img_file, crop_points), save=False   )
                #---------------------------------------------------------------------------------------------------------------------------------------

                new_items.append( new_item )                
                created_objects = Image.objects.bulk_create( new_items )
                Belonging.objects.bulk_create( [ Belonging(gallery=gallery, content_id = item.id) for item in new_items] )
                for img in created_objects:
                    img.create_tags(gallery = gallery) #<-We save the notifications for bulk create

                return JsonResponse({'status': 'ok', 'gallery': gallery.id})

        else:
            form = MultiUploadImageForm()

        return render(
            request,
            'upload/create.html',
            {'form': form}
        )

#I THOUGHT THIS COULD BE THE FUNCTION TAKING TIME BUT IT IS NOT:
def ProcessImageFile(img_file, crop_points):
    img = ImageProcessor.open(img_file)
    cropped_img = img.crop( ( int(crop_points[0]), int(crop_points[1]), int(crop_points[2]), int(crop_points[3])))
    img_io = BytesIO()
    cropped_img.save( img_io, format='JPEG', quality=100)
    return ContentFile( img_io.getvalue())

我已经尝试使用 Celery 在单独的任务中处理文件上传,但这里的问题是将请求或图像文件传递给任务,因为它们必须被序列化。无论如何,我猜这里有些东西效率低下,这个简单的 View 不应该花费超过 30 秒的时间在 S3 中上传五张图片并返回响应。也许解决方案是将所有图像一起批量发送到 S3,或者以其他方式保存它们,我不知道。

最佳答案

我认为你必须这样前进:

  • 首先,将您案例中的任何类型的文件(例如 images)保存到本地目录中。

  • 之后,您可以使用Celery 将此文件上传或传输到其他地方,可以是S3 等远程存储位置。 .

这是一个开源应用程序,它正在执行您真正需要的任务;

Visit Reference: django-queued-storage

安装和使用也非常简单明了:

pip install django-queued-storage

关于python - 查看使用 Heroku 和 S3 处理图像花费太多时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58428108/

相关文章:

python - 一个一个地遍历 ndarray 的所有元素

Django 1.11 禁止直接赋值给多对多集合的前端

django - 如何在 django 管理中将多个图像上传与 UPLOADIFY 集成?

heroku - 有没有办法将部署在 Heroku 上的 MeteorJS 应用程序的所有流量从 HTTP 重定向到 HTTPS?

python - 将 numpy 数组中的一列设置为零

python - 应用装饰器的弃用

Python:如何使用 splinter/Browser 一次性填写表格?

python - 模块未找到错误: importlib when pushing to Heroku despite local changes

heroku - 在 Heroku 上运行跨区域部署的最佳方式是什么?

python - ERROR : Command errored out with exit status 1: python setup. py Egg_info 检查日志以获取完整的命令输出。 [ flask ]