Python Reddis 队列 ValueError:来自 __main__ 模块的函数无法由工作人员处理 [英] Python Reddis Queue ValueError: Functions from the __main__ module cannot be processed by workers

查看:49
本文介绍了Python Reddis 队列 ValueError:来自 __main__ 模块的函数无法由工作人员处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 python-rq 在 redis 中排队一项基本工作,但它抛出了这个错误

I'm trying to enqueue a basic job in redis using python-rq, But it throws this error

ValueError: 来自 ma​​in 模块的函数不能被 worker 处理"

"ValueError: Functions from the main module cannot be processed by workers"

这是我的程序:

import requests

def count_words_at_url(url):
    resp = requests.get(url)
    return len(resp.text.split())

from rq import Connection, Queue
from redis import Redis
redis_conn = Redis()
q = Queue(connection=redis_conn)
job = q.enqueue(count_words_at_url, 'http://nvie.com')
print job

推荐答案

将提供的代码分解为两个文件:count_words.py:

Break the provided code to two files: count_words.py:

import requests

def count_words_at_url(url):
    resp = requests.get(url)
    return len(resp.text.split())

ma​​in.py(您将在其中导入所需的函数):

and main.py (where you'll import the required function):

from rq import Connection, Queue
from redis import Redis
from count_words import count_words_at_url # added import!
redis_conn = Redis()
q = Queue(connection=redis_conn)
job = q.enqueue(count_words_at_url, 'http://nvie.com')
print job

我总是将任务与将这些任务运行到不同文件的逻辑分开.这只是更好的组织.另请注意,您可以定义一类任务并从该类导入/安排任务,而不是我上面建议的(过度简化的)结构.这应该让你去..另请参阅此处以确认您不是第一个遇到此示例的人.RQ 很棒一旦你掌握了它.

I always separate the tasks from the logic running those tasks to different files. It's just better organization. Also note that you can define a class of tasks and import/schedule tasks from that class instead of the (over-simplified) structure I suggest above. This should get you going.. Also see here to confirm you're not the first to struggle with this example. RQ is great once you get the hang of it.

这篇关于Python Reddis 队列 ValueError:来自 __main__ 模块的函数无法由工作人员处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆