python fastapi celery hdfs 异步上传

在Python中使用FastAPI和Celery结合HDFS(Hadoop Distributed File System)进行异步上传图文教程,可以分为以下几个步骤来实现:

步骤 1: 环境准备

  1. 安装必要的库

    复制代码
    pip install fastapi uvicorn celery hdfs
  2. 安装Redis

    Celery 使用 Redis 作为消息代理,确保 Redis 已经安装并运行。

    复制代码
    # 安装Redis 
    sudo apt-get install redis-server

步骤 2: 创建FastAPI应用

创建一个main.py文件,设置FastAPI应用:

复制代码
from fastapi import FastAPI, HTTPException, File, UploadFile 
from celery import Celery 
import os app = FastAPI() 

# 设置Celery 
celery = Celery('tasks', 
                broker='redis://localhost:6379/0', 
                backend='redis://localhost:6379/0') 

@app.post("/upload/") 
async def upload_file(file: UploadFile = File(...)): 
    try: 
        task = upload_to_hdfs.delay(file.filename, file.content) 
        return {"status": "success", "task_id": task.id} 
    except Exception as e: 
        raise HTTPException(status_code=500, detail=str(e)

步骤 3: 创建Celery任务

在同一个目录下创建一个tasks.py文件,定义上传到HDFS的任务:

复制代码
from celery import shared_task 
from hdfs import InsecureClient 
import io 

# 配置HDFS客户端 
client = InsecureClient('http://localhost:50070', 
                        user='your_username') 

@shared_task(bind=True) 
def upload_to_hdfs(self, filename, file_content): 
    with io.BytesIO(file_content) as buffer: 
        buffer.seek(0) # 重置指针位置到文件开始处 
        client.upload(f'/user/your_username/{filename}', buffer) 
        return f"File {filename} uploaded successfully."

步骤 4: 运行你的应用和Celery worker

  1. 启动Redis服务器‌ (如果尚未启动):

    复制代码
    redis-server
  2. 启动Celery worker‌:

    复制代码
    celery -A tasks worker --loglevel=info
  3. 运行FastAPI应用‌:

    复制代码
    uvicorn main:app --reload

步骤 5: 测试上传功能

使用Postman或者curl来测试上传功能:

复制代码
curl -X POST http://127.0.0.1:8000/upload/ -F "file=@path_to_your_file" -H "Content-Type: multipart/form-data"

确保替换path_to_your_file为你的文件路径。你应该会看到一个包含任务ID的响应。你可以使用这个ID来跟踪任务状态。

步骤 6: (可选) 监控任务状态

你可以在FastAPI中添加一个端点来获取任务状态:

复制代码
@app.get("/status/{task_id}") 
async def get_task_status(task_id: str): 
    task = upload_to_hdfs.AsyncResult(task_id) # 使用AsyncResult以支持异步查询状态 
    if task.state == 'PENDING': 
        response = {"state": task.state, "status": "Pending..."} 
    elif task.state != 'FAILURE': 
        response = {"state": task.state, "status": task.info} # 获取任务结果或错误信息等。 
    else: # 任务失败时,获取失败的原因。 
        response = {"state": task.state, "status": str(task.info)} # 这里可以更详细地获取异常信息等。 
    return response

这样,你可以通过访问/status/{task_id}来检查任务的执行状态

相关推荐
Dxy12393102162 小时前
Python多线程如何操作全局变量:从踩坑到最佳实践
python
SilentSamsara2 小时前
RAG 系统入门:LangChain/LlamaIndex + Chroma 向量数据库的检索增强实战
数据库·人工智能·python·青少年编程·langchain
码云骑士2 小时前
06-Python装饰器从入门到源码(上)-闭包与自由变量
开发语言·python
码云骑士2 小时前
10-Python运行时内存模型-栈帧-堆-引用计数-GC分代回收的全景图
开发语言·python
码云骑士2 小时前
02-Python可变对象与不可变对象(上)-赋值陷阱与函数传参的暗坑
开发语言·python
疯狂学习GIS2 小时前
基于Python earthaccess库批量下载全球MODIS GPP(MOD17A2HGF)数据
python·脚本·批量下载·遥感影像·nasa·earthdata·自动处理
至乐活着2 小时前
用DeepSeek打造你自己的智能问答系统:从零到一的完整指南
python·deepseek·ai应用开发·智能问答系统·api教程
AI创界者2 小时前
【解压即用】Scail-2 视频动作迁移一键整合包:8G显存通吃50系,长视频/多人/精准目标替换全攻略
人工智能·python·aigc·音视频