[Python] How can I access current_user outside of an route. FLASK API

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

  1. Stack

    Stack Membro Participativo

    Im trying to make a polling mechanism, so im making a background process using "ThreadPoolExecutor", I added a status boolean to my users table so they will only be able to send 1 request at time, but i´ve ran into a problem where I cant change the current_user.status to False inside of the background process is over since its outside of an route.

    def background_translation(file_content, w_model, transcription, model, pitch, speech_rate, user_id):
    try:
    srt_file = whisper_transcribe(file_content, w_model, transcription)
    audio = text_to_speech(srt_file, model, pitch, speech_rate)
    output = add_stream(audio, file_content)

    # Save as user_id.mp4
    destination = os.path.join(CONTENT_FOLDER, f"{user_id}.mp4")
    shutil.move(output, destination)

    print(f"Translation complete: saved to {destination}")

    except Exception as e:
    print("BGT error during translation:", e)


    u/bp.route('/translator', methods=['POST'])
    u/login_or_token_required
    def translator(user):

    #inputs...

    user_id = current_user.id

    start_process(current_user)

    file_extension = secure_filename(filepath.filename)
    file_content = os.path.join(UPLOAD_FOLDER, file_extension)
    filepath.save(file_content)


    print("executor.submit")
    executor.submit(
    background_translation,
    file_content,
    w_model,
    transcription,
    model,
    pitch,
    speech_rate,
    user_id
    )
    print("Sent")

    return jsonify({
    "message": "Translation started",
    }), 200


    I tried to access the query by user_id

    user = User.query.get(user_id)

    Continue reading...

Compartilhe esta Página