异步任务与消息队列 logo

异步任务与消息队列

Organization
microwind
异步任务与消息队列

当设计异步任务系统时,分析消息队列策略,优化任务处理性能,解决异步通信问题。验证任务架构,设计消息模式,和最佳实践。

Overview

Publishermicrowind
Repositoryai-skills
Skill name异步任务与消息队列
Stars
93
Forks
25
Bundled files
2
LicenseMIT
Links
  • Markdown instructions

    A SKILL.md file the model loads on demand, so it only costs tokens when a request actually matches.

  • Works with any LLM

    AI skills are plain Markdown, not provider-specific code, so this works with GPT, Claude, Gemini, Grok, or a local model.

  • 2 bundled files

    Scripts, templates, and references the model can read while it works. Files are read-only and never executed.

  • Open source

    Published by microwind on GitHub. Read the source before you install it.

Installation

Install the 异步任务与消息队列 AI skill in TypingMind to use it with any LLM, or drop it into another agent that reads SKILL.md.

1

Install in TypingMind

TypingMind installs a skill straight from its GitHub folder — it reads SKILL.md, bundles the resource files, and stores the result locally.

  1. Open the app and go to Plugins → Skills.
  2. Choose "Install from GitHub".
  3. Paste the skill folder URL below and confirm.
    https://github.com/microwind/ai-skills/tree/main/backend/async-tasks
  4. Enable the skill in any chat where you want it available.
Plugins → Skills → Add skill → From GitHub URL, then paste the folder URL and press Continue.
2

Install in another agent

Any agent that reads the Agent Skills format can use this skill — copy the folder into that agent's skills directory.

Claude Code — .claude/skills
git clone --depth 1 https://github.com/microwind/ai-skills.git /tmp/ai-skills
mkdir -p .claude/skills
cp -r /tmp/ai-skills/backend/async-tasks .claude/skills/microwind
Restart Claude Code after copying so it picks up the new skill.

Use it in TypingMind

Enable 异步任务与消息队列 in any TypingMind chat and the model takes it from there. Its name and description sit in the system prompt, and the moment a request matches, the model loads the full instructions itself — you never invoke it by hand, and it costs no tokens until it is actually used.

The model loads 异步任务与消息队列 on its own as soon as a request matches it.

Works with any AI model

AI skills are plain Markdown instructions rather than provider-specific code, so 异步任务与消息队列 is not tied to the model it was written for. Install it once in TypingMind and use it with GPT-5, Claude, Gemini, Grok, DeepSeek, Mistral, Llama, or a local model you run yourself — all on your own API keys.

  • Loaded only when it is needed

    The system prompt carries just the name and description. The instructions are fetched on the first matching request, so an idle skill costs nothing.

  • Switch models mid-chat

    Because the skill is instructions rather than code, changing model does not break it — the next model reads the same SKILL.md.

Skill instructions

This is the SKILL.md content the model loads. Read it before installing — a skill is instructions your model will follow.

异步任务与消息队列技能

概述

异步任务和消息队列是构建高性能、可扩展系统的关键技术。不当的异步设计会导致任务丢失、性能瓶颈和系统复杂性增加。在设计异步系统前需要仔细分析需求。

核心原则: 好的异步设计应该提升系统吞吐量和响应性,同时保证任务的可靠性。坏的异步设计会增加系统复杂性,甚至导致数据不一致。

何时使用

始终:

  • 设计高并发系统时
  • 处理耗时任务时
  • 系统解耦和微服务通信时
  • 设计任务调度系统时
  • 处理批量数据时
  • 实现事件驱动架构时

触发短语:

  • "这个任务需要异步处理吗?"
  • "消息队列选型建议"
  • "如何保证任务不丢失?"
  • "异步任务失败了怎么办?"
  • "RabbitMQ和Kafka怎么选?"
  • "任务积压怎么处理?"

异步任务技能功能

消息模式分析

  • 点对点模式(Point-to-Point)
  • 发布订阅模式(Publish-Subscribe)
  • 请求响应模式(Request-Reply)
  • 工作队列模式(Work Queue)
  • 路由模式(Routing)

任务处理策略

  • 任务优先级管理
  • 失败重试机制
  • 死信队列处理
  • 任务去重策略
  • 延迟任务调度

性能优化检查

  • 消息吞吐量分析
  • 队列容量规划
  • 消费者扩展策略
  • 消息持久化配置
  • 监控告警设置

常见异步设计问题

消息丢失问题

问题:
消息在传输过程中丢失

后果:
- 业务数据不一致
- 任务执行失败
- 用户体验差
- 数据完整性问题

解决方案:
- 消息持久化
- 确认机制(ACK)
- 事务消息
- 消息去重

任务积压问题

问题:
生产者速度超过消费者速度

后果:
- 内存占用过高
- 处理延迟增加
- 系统响应变慢
- 可能的OOM风险

解决方案:
- 增加消费者数量
- 优化处理逻辑
- 分批处理机制
- 限流控制

消费者重复消费

问题:
同一条消息被多次处理

后果:
- 数据重复计算
- 业务逻辑异常
- 资源浪费
- 数据不一致

解决方案:
- 幂等性设计
- 消息唯一ID
- 处理状态记录
- 分布式锁控制

消息队列技术选型

RabbitMQ vs Kafka vs Redis

特性RabbitMQKafkaRedis
消息模式多种模式发布订阅简单队列
吞吐量中等极高高
持久化支持支持可选
复杂度中等较高简单
适用场景企业应用、复杂路由大数据、日志流轻量级、缓存

选型指南

RabbitMQ适用场景:

  • 复杂的路由需求
  • 需要消息确认机制
  • 企业级应用集成
  • 多种消息模式

Kafka适用场景:

  • 大数据流处理
  • 日志收集和分析
  • 事件溯源架构
  • 高吞吐量需求

Redis适用场景:

  • 轻量级任务队列
  • 实时通信
  • 缓存和队列结合
  • 简单的异步处理

异步任务架构设计

任务分发模式

架构: 生产者 -> 消息队列 -> 多个消费者

适用场景:
- 任务处理时间较长
- 需要水平扩展
- 提高系统吞吐量

实现要点:
- 任务负载均衡
- 消费者健康检查
- 动态扩缩容
- 监控队列长度

事件驱动模式

架构: 事件发布者 -> 事件总线 -> 多个订阅者

适用场景:
- 系统解耦
- 微服务通信
- 事件溯源
- CQRS架构

实现要点:
- 事件定义标准化
- 版本兼容性
- 事件存储
- 重放机制

工作流模式

架构: 任务编排器 -> 多个步骤队列 -> 结果聚合

适用场景:
- 复杂业务流程
- 多步骤处理
- 条件分支
- 人工审批

实现要点:
- 工作流引擎
- 状态机管理
- 超时处理
- 补偿机制

代码实现示例

RabbitMQ实现

python
import pika
import json
from functools import wraps

# 连接配置
def get_connection():
    credentials = pika.PlainCredentials('username', 'password')
    connection = pika.BlockingConnection(
        pika.ConnectionParameters('localhost', credentials=credentials)
    )
    return connection

# 生产者
def publish_task(queue_name, task_data):
    """发布任务到队列"""
    connection = get_connection()
    channel = connection.channel()
    
    # 声明队列
    channel.queue_declare(queue=queue_name, durable=True)
    
    # 发布消息
    channel.basic_publish(
        exchange='',
        routing_key=queue_name,
        body=json.dumps(task_data),
        properties=pika.BasicProperties(
            delivery_mode=2,  # 消息持久化
        )
    )
    connection.close()

# 消费者
def process_task(queue_name, callback):
    """消费队列中的任务"""
    connection = get_connection()
    channel = connection.channel()
    
    channel.queue_declare(queue=queue_name, durable=True)
    
    # 设置QoS,每次只处理一条消息
    channel.basic_qos(prefetch_count=1)
    
    def wrapper(ch, method, properties, body):
        try:
            task_data = json.loads(body)
            result = callback(task_data)
            
            # 处理成功,发送ACK
            ch.basic_ack(delivery_tag=method.delivery_tag)
            return result
        except Exception as e:
            # 处理失败,拒绝消息(可重试)
            ch.basic_nack(delivery_tag=method.delivery_tag, requeue=True)
            print(f"Task processing failed: {e}")
    
    channel.basic_consume(queue=queue_name, on_message_callback=wrapper)
    channel.start_consuming()

# 幂等性装饰器
def idempotent(task_id_key='task_id'):
    """确保任务幂等性"""
    def decorator(func):
        @wraps(func)
        def wrapper(task_data, *args, **kwargs):
            task_id = task_data.get(task_id_key)
            if not task_id:
                raise ValueError("Task ID is required for idempotency")
            
            # 检查任务是否已处理
            if is_task_processed(task_id):
                print(f"Task {task_id} already processed")
                return get_task_result(task_id)
            
            # 标记任务开始处理
            mark_task_processing(task_id)
            
            try:
                result = func(task_data, *args, **kwargs)
                # 标记任务完成
                mark_task_completed(task_id, result)
                return result
            except Exception as e:
                # 标记任务失败
                mark_task_failed(task_id, str(e))
                raise
        return wrapper
    return decorator

# 使用示例
@idempotent('task_id')
def process_email_task(task_data):
    """处理邮件发送任务"""
    recipient = task_data['recipient']
    subject = task_data['subject']
    content = task_data['content']
    
    # 模拟邮件发送
    print(f"Sending email to {recipient}: {subject}")
    # send_email(recipient, subject, content)
    
    return {"status": "sent", "recipient": recipient}

Celery实现

python
from celery import Celery
from celery.schedules import crontab

# 配置Celery
app = Celery('tasks', broker='redis://localhost:6379/0')

# 基础任务
@app.task(bind=True, max_retries=3)
def process_data_task(self, data):
    """处理数据的异步任务"""
    try:
        # 业务逻辑处理
        result = expensive_operation(data)
        return {'status': 'success', 'result': result}
    except Exception as exc:
        # 重试机制
        raise self.retry(exc=exc, countdown=60)

# 定时任务
@app.task
def cleanup_expired_data():
    """清理过期数据的定时任务"""
    # 清理逻辑
    expired_count = delete_expired_records()
    return f"Cleaned up {expired_count} expired records"

# 任务调度
app.conf.beat_schedule = {
    'cleanup-expired-data': {
        'task': 'tasks.cleanup_expired_data',
        'schedule': crontab(minute=0, hour=2),  # 每天凌晨2点执行
    },
}

监控和运维

关键指标

  • 队列长度: 监控消息积压情况
  • 处理速度: 消费者处理速率
  • 错误率: 任务失败比例
  • 延迟时间: 消息端到端延迟
  • 资源使用: CPU、内存、网络

监控工具

  • RabbitMQ: Management UI, Prometheus插件
  • Kafka: Kafka Manager, Confluent Control Center
  • Celery: Flower监控面板
  • 自定义: Grafana + Prometheus

运维最佳实践

  1. 容量规划: 根据业务量预估资源需求
  2. 监控告警: 设置合理的告警阈值
  3. 备份恢复: 定期备份重要数据
  4. 性能调优: 根据监控数据优化配置
  5. 故障演练: 定期进行故障恢复演练

故障处理

常见故障及解决方案

  • 连接断开: 实现重连机制
  • 消息积压: 增加消费者或优化处理逻辑
  • 重复消费: 实现幂等性处理
  • 消息丢失: 启用持久化和确认机制
  • 性能下降: 分析瓶颈并优化

应急预案

  1. 服务降级: 关键任务优先处理
  2. 流量控制: 限制生产者发送速率
  3. 数据补偿: 事后数据修复机制
  4. 服务恢复: 快速恢复服务可用性

相关技能

  • error-handling-logging - 错误处理和日志记录
  • performance-profiler - 性能分析和优化
  • architecture-analyzer - 系统架构设计
  • monitoring-alerting - 监控和告警系统

Bundled files

The model reads these on demand while the skill is loaded. They are exposed as readable files and are never executed.

Frequently asked questions

What does the 异步任务与消息队列 AI skill do?

当设计异步任务系统时,分析消息队列策略,优化任务处理性能,解决异步通信问题。验证任务架构,设计消息模式,和最佳实践。

Why use 异步任务与消息队列 on TypingMind?

Because you install it once and use it with any model. 异步任务与消息队列 is plain Markdown rather than provider-specific code, so the same skill runs on GPT-5, Claude, Gemini, Grok, or a local model — and you can switch model mid-chat without it breaking. TypingMind runs on your own API keys, so you pay providers directly instead of a per-seat subscription, and your skills and chats stay in your own storage.

How do I install 异步任务与消息队列 in TypingMind?

Open Plugins → Skills → Install from GitHub in TypingMind and paste https://github.com/microwind/ai-skills/tree/main/backend/async-tasks. TypingMind reads its SKILL.md and bundles its files and installs it as a skill you can enable per chat.

Which AI models can use 异步任务与消息队列?

Any model you connect in TypingMind. AI skills are plain Markdown instructions rather than provider-specific code, so GPT, Claude, Gemini, Grok, and local models can all load this skill when a request matches it.

How many AI models can I use with 异步任务与消息队列?

As many as you like. As long as a model supports skills, you can use 异步任务与消息队列 with it — GPT, Claude, Gemini, Grok, DeepSeek, Mistral, Llama and more — all on TypingMind with your own API keys.

Is the 异步任务与消息队列 AI skill free?

Yes. It is published on GitHub by microwind under the MIT license. You only pay your own AI provider for the tokens you use.

What are AI skills?

An AI skill is a reusable instruction bundle that teaches an AI model how to do one specific task. It follows the open Agent Skills format: a SKILL.md file with a name and description, plus any scripts, templates or reference files the model may need. The model reads the instructions only when your request matches the skill, so an installed skill costs nothing until it is used.

How are AI skills different from plugins or MCP servers?

A plugin or MCP server gives a model new tools to call — code that runs somewhere and returns a result. An AI skill gives the model knowledge and process instead: how to approach a task, which steps to follow, what good output looks like. Skills are plain Markdown, so they need no server, no API key and no runtime, and they work with any model.

View all

Set up your own AI workspace now

Get notified about new features and future giveaways by subscribing to our newsletter 👇