You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ubuntu启动RQ Worker报错:时间数据格式不匹配求助

问题:RQ Worker启动失败,解析空时间字符串报错

问题场景

在Ubuntu系统中执行rq worker启动RQ Worker时触发报错,任务已成功加入本地Redis队列,但Worker无法正常运行,系统显示0个Worker。

错误栈信息

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/rq/utils.py", line 169, in utcparse
    return datetime.datetime.strptime(string, _TIMESTAMP_FORMAT)
  File "/usr/lib/python3.8/_strptime.py", line 568, in _strptime_datetime
    tt, fraction, gmtoff_fraction = _strptime(data_string, format)
  File "/usr/lib/python3.8/_strptime.py", line 349, in _strptime
    raise ValueError("time data %r does not match format %r" %
ValueError: time data '' does not match format '%Y-%m-%dT%H:%M:%S.%fZ'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/rq/worker.py", line 508, in work
    result = self.dequeue_job_and_maintain_ttl(timeout)
  File "/usr/lib/python3/dist-packages/rq/worker.py", line 574, in dequeue_job_and_maintain_ttl
    result = self.queue_class.dequeue_any(self.queues, timeout,
  File "/usr/lib/python3/dist-packages/rq/queue.py", line 539, in dequeue_any
    job = job_class.fetch(job_id, connection=connection)
  File "/usr/lib/python3/dist-packages/rq/job.py", line 303, in fetch
    job.refresh()
  File "/usr/lib/python3/dist-packages/rq/job.py", line 515, in refresh
    self.restore(data)
  File "/usr/lib/python3/dist-packages/rq/job.py", line 478, in restore
    self.started_at = str_to_date(obj.get('started_at'))

相关代码

任务定义(hello.py)

def add(x, y):
    return x + y

主程序(main.py)

from flask import Flask
from redis import Redis

from hello import add
from rq import Queue

q = Queue(connection=Redis())


app = Flask(__name__)

@app.route("/")
def hello():
    q.enqueue(add, 3, 7)
    return "Task enqueued"


if __name__ == "__main__":
    app.run(debug=True)

解决方案

这个错误是因为Redis队列中存在损坏的任务数据——某个任务的started_at字段是空字符串,RQ解析时无法匹配时间格式导致崩溃。

解决步骤:

  • 清理Redis中的损坏任务数据:
    1. 进入Redis命令行:redis-cli
    2. 查看所有RQ相关键:KEYS "rq:*"
    3. 删除相关队列和任务键,比如默认队列:DEL rq:queue:default,或具体任务ID对应的键:DEL rq:job:xxx
      (如果数据无保留价值,可直接执行FLUSHDB清空当前Redis数据库,谨慎操作)
  • 重启RQ Worker:rq worker
  • 重新触发任务,验证Worker运行状态

另外,旧版本RQ可能存在空时间字段处理的bug,建议升级到最新稳定版:pip install --upgrade rq

内容的提问来源于stack exchange,提问作者Gaming Fury

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 15:01:05