我已经安装并运行了 redis(Redis version=6.0.9
在 Ubuntu 18.04 机器上),并按照这个简单的指南来安排工作,rq
但是一旦我启动app.py
redis worker 就会失败:
10:31:17 RQ worker u'rq:worker:c73feeafc20f4f0c8baff1e5225e73f1' started, version 1.0
10:31:17 *** Listening on default...
10:31:18 Worker c73feeafc20f4f0c8baff1e5225e73f1: found an unhandled exception, quitting...
Traceback (most recent call last):
File "/home/enrico/.local/lib/python2.7/site-packages/rq/worker.py", line 470, in work
result = self.dequeue_job_and_maintain_ttl(timeout)
File "/home/enrico/.local/lib/python2.7/site-packages/rq/worker.py", line 514, in dequeue_job_and_maintain_ttl
job_class=self.job_class)
File "/home/enrico/.local/lib/python2.7/site-packages/rq/queue.py", line 468, in dequeue_any
job = job_class.fetch(job_id, connection=connection)
File "/home/enrico/.local/lib/python2.7/site-packages/rq/job.py", line 287, in fetch
job.refresh()
File "/home/enrico/.local/lib/python2.7/site-packages/rq/job.py", line 428, in refresh
self.started_at = to_date(as_text(obj.get('started_at')))
File "/home/enrico/.local/lib/python2.7/site-packages/rq/job.py", line 411, in to_date
return utcparse(as_text(date_str))
File "/home/enrico/.local/lib/python2.7/site-packages/rq/utils.py", line 175, in utcparse
return datetime.datetime.strptime(string, '%Y-%m-%dT%H:%M:%SZ')
File "/usr/lib/python2.7/_strptime.py", line 332, in _strptime
(data_string, format))
ValueError: time data '' does not match format '%Y-%m-%dT%H:%M:%SZ'
这里tasks.py
:
from datetime import datetime, timedelta
import time
def print_task(seconds):
print("Starting task")
for num in range(seconds):
print(num, ". Hello World!")
time.sleep(1)
print("Task completed")
def print_numbers(seconds):
print("Starting num task")
for num in range(seconds):
print(num)
time.sleep(1)
print("Task to print_numbers completed")
这里app.py
:
from datetime import datetime, timedelta
import time
from redis import Redis
from rq import Queue
import tasks
queue = Queue(connection=Redis())
def queue_tasks():
queue.enqueue(tasks.print_task, 5)
queue.enqueue_in(timedelta(seconds=10), tasks.print_numbers, 5)
def main():
queue_tasks()
if __name__ == "__main__":
main()
这里我使用的命令:
python3.8 -m venv venv
source venv/bin/activate
pip install rq ### Successfully installed click-7.1.2 redis-3.5.3 rq-1.7.0
python app.py
我打开第二个窗口并运行rq worker
,我在这篇文章的第一部分失败了。
我试图用谷歌搜索,但这篇文章只是说他更新了他的 Windows 操作系统,这是一个已解决的错误……有人知道如何解决这个问题吗?