[Python] Will requests to my site lag and work slowly in django while waiting for celery...

Discussão em 'Python' iniciado por Stack, Julho 11, 2025.

  1. Stack

    Stack Membro Participativo

    I use django to create pdf to docx converter using pdf2docx library, and I need to wait a celery task to done and get result from it. Will my site lag and work slowly if a lot of users use it, and how can I do it better? here my views and celery code

    views.py '''

    from django.shortcuts import render
    from django.http import JsonResponse, HttpResponse
    from django.http.request import HttpRequest

    import tempfile

    import os
    from .forms import FileUploadForm
    from django.views.decorators.csrf import csrf_protect
    from . import tasks

    from celery.result import AsyncResult

    @csrf_protect
    def main_page(request: HttpRequest):
    if request.method == "GET":
    # get
    form = FileUploadForm(request.POST, request.FILES)
    context = {
    "form": form
    }
    return render(request, 'main/main_page.html', context)

    if request.method == 'POST' and request.FILES.get('file'):
    form = FileUploadForm(request.POST, request.FILES)

    if form.is_valid():
    # get file
    file = request.FILES['file']
    size_limit = 2 * 1024 * 1024

    # save pdf
    with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as temp_file:
    if file.size > size_limit:
    for chunk in file.chunks():
    temp_file.write(chunk)
    else:
    temp_file.write(file.read())
    temp_pdf_path = temp_file.name

    # start convertor task
    task: AsyncResult = tasks.convert_pdf_to_docx.delay(temp_pdf_path)
    # get docx path
    temp_docx_path = task.wait(timeout=None, interval=0.5)

    converted_file_name = str(file).replace(".pdf", "")
    # read docx file and set it in response
    with open(temp_docx_path, 'rb') as docx_file:
    file_data = docx_file.read()
    response = HttpResponse(file_data, content_type='application/vnd.openxmlformats-officedocument.wordprocessingml.document')

    response['Content-Disposition'] = f'attachment; filename={converted_file_name}.docx'
    response['Content-Length'] = len(file_data) # calculate length of content
    # Clean up temp files
    os.remove(temp_pdf_path)
    os.remove(temp_docx_path)

    return response
    else:
    context = {
    "form": form,
    "submit_error": True,
    }
    return render(request, 'main/main_page.html', context)


    '''

    tasks.py '''

    from celery import shared_task
    from pdf2docx import Converter

    @shared_task()
    def convert_pdf_to_docx(temp_pdf_path):
    # Convert to DOCX
    temp_docx_path = temp_pdf_path.replace(".pdf", ".docx")
    cv = Converter(temp_pdf_path)
    cv.convert(temp_docx_path)
    cv.close()
    return temp_docx_path


    '''

    to run celery I use: '''

    celery -A pdfconverter worker --loglevel=info -P gevent


    '''

    Continue reading...

Compartilhe esta Página