Skip to main content

xxl-jobs 的python客户端实现

Package version Supported Python versions Coverage

使用pyxxl可以方便的把Python写的方法注册到XXL-JOB中,使用XXL-JOB-ADMIN管理Python定时任务和周期任务

实现原理:通过XXL-JOB提供的RESTful API接口进行对接

注意!!!如果用同步的方法,请查看下面同步任务注意事项。

已经支持的功能

  • 执行器注册到job-admin
  • task注册,类似于flask路由装饰器的用法
  • 任务的管理(支持在界面上取消,发起等操作,任务完成后会回调admin)
  • 所有阻塞策略的支持
  • 异步支持(推荐)
  • job-admin上查看日志

已经测试过的XXL-JOB版本

3.3.2,2.4.0,2.3.0,2.2.0

如遇到不兼容的情况请issue告诉我XXL-JOB版本和对应的问题我会尽量适配

如何使用

pip install pyxxl

# 如果日志需要写入redis
pip install "pyxxl[redis]"

# 如果需要从.env加载配置
pip install "pyxxl[dotenv]"

# 安装所有功能
pip install "pyxxl[all]"
import asyncio

from pyxxl import ExecutorConfig, PyxxlRunner

config = ExecutorConfig(
    xxl_admin_baseurl="http://localhost:8080/xxl-job-admin/api/",
    executor_app_name="xxl-job-executor-sample"
)

app = PyxxlRunner(config)

@app.register(name="demoJobHandler")
async def test_task():
    await asyncio.sleep(5)
    return "成功..."

# 如果你代码里面没有实现全异步,请使用同步函数,不然会阻塞其他任务
@app.register(name="xxxxx")
def test_task3():
    return "成功3"


app.run_executor()

更多示例和接口文档请参考 PYXXL文档 ,具体代码在example文件夹下面

如果executor服务无法直连xxl-admin,请参考PYXXL配置修改executor_listen_host

监控指标

pip install "pyxxl[metrics]"

安装metrics扩展后,执行器会自动加载prometheus的指标监控功能

访问地址为: http://executor_listen_host:executor_listen_port/metrics

同步任务注意事项

同步任务会放到线程池中运行,无法正确接受cancel信号和timeout配置

如果需要使用同步任务并且无法控制(预料)里面执行时间,又需要进行超时和cancel功能的,需要接受pyxxl发出的cancel_event,当该cancel_event被设置时需要中断任务,下面是一个案例:

...

@app.register(name="sync_func")
def sync_loop_demo():
    # 如果同步任务里面有循环,为了支持cancel操作,必须每次都判断g.cancel_event.
    task_params_list = []
    while not g.cancel_event.is_set() and task_parasm_list:
        params = task_params_list.pop()
        time.sleep(3)
    return "ok"

# 如下代码会造成线程池里的线程被永远占用,timeout cancel全部不生效
@app.register(name="sync_func2")
def sync_loop_demo2():
    while True:
        time.sleep(3) # 模拟你运行的任务
    return "ok"

其他

  • 由于种种3.9之后才加入的语法与特性,减少开发与适配成本,计划后续版本不再适配Python3.9以下版本,0.3.0最后一个支持Python3.8的版本

  • 访问xxl-admin接口时支持从环境变量读取代理(列如HTTP_PROXY)

已知问题

  1. 界面上显示的执行时间其实是任务回调的时间,而不是真正开始的时间.这是XXL-JOB的bug,pyxxl这边已经传了正确的执行时间过去,XXL-JOB没有按预期解析直接取了当前时间
  2. 日志的实时拉取进度有问题,暂时不好解决,进行中的任务刷新整个页面

Star History

Star History Chart

开发人员

下面是开发人员如何快捷的搭建开发调试环境

启动xxl的调度中心

./init_dev_env.sh

http://127.0.0.1:8080/xxl-job-admin/

admin/123456

启动执行器

uv sync --all-extras
# 修改app.py中相关的配置信息,然后启动
python example/executor_app.py

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyxxl-0.4.8.tar.gz (549.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyxxl-0.4.8-py3-none-any.whl (50.0 kB view details)

Uploaded Python 3

File details

Details for the file pyxxl-0.4.8.tar.gz.

File metadata

  • Download URL: pyxxl-0.4.8.tar.gz
  • Upload date:
  • Size: 549.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for pyxxl-0.4.8.tar.gz
Algorithm Hash digest
SHA256 110a67aa91d8da87bf90563089143fbaa4e1ed0687e904da78f3ce1a63e1e1b0
MD5 51b7582b6616dc0fb845a38af75d54d7
BLAKE2b-256 b3eb7b625375fee57125008944c44c783623b2b60724735797d16e0736a4e7e9

See more details on using hashes here.

File details

Details for the file pyxxl-0.4.8-py3-none-any.whl.

File metadata

  • Download URL: pyxxl-0.4.8-py3-none-any.whl
  • Upload date:
  • Size: 50.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for pyxxl-0.4.8-py3-none-any.whl
Algorithm Hash digest
SHA256 ca9dbded407c0eda3ba7be4443f8c7074be58ddbc979e4f526cc64f981455c63
MD5 7e18bc5bacff5056bc88bbabdc4940cd
BLAKE2b-256 81c606218856f9e456b24ed6c0a6b852e86fe505ece648b21ec9c70b8b88a229

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.4.8 This release

2 files

0.4.7

2 files

0.4.6

2 files

0.4.5

2 files

0.4.4

2 files

0.4.3

2 files

0.4.2

2 files

0.4.1

2 files

0.4.0

2 files

0.3.8

2 files

0.3.7

2 files

0.3.6

2 files

0.3.5

2 files

0.3.4

2 files

0.3.3

2 files

0.3.2

2 files

0.3.1

2 files

0.3.0

2 files

0.2.2

2 files

0.2.1

2 files

0.1.7

2 files

0.1.6

2 files

0.1.5

2 files

0.1.4

2 files

0.1.3

1 file

0.1.2

1 file

0.1.1

1 file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page