簡體   English   中英

RQ AttributeError: 'module' 對象沒有屬性

[英]RQ AttributeError: 'module' object has no attribute

我正在嘗試使用 RQ 創建后台作業:

import django_rq                                                         


    def _send_password_reset_email_async(email):                             
        print(email)                                                         

    # Django admin action to send reset password emails                                                                 
    def send_password_reset_email(modeladmin, request, queryset):            
        for user in queryset:                                                
            django_rq.enqueue(_send_password_reset_email_async, user.email)  
    send_password_reset_email.short_description = 'Send password reset email'

我一直收到這個錯誤,似乎我在做一些愚蠢的事情?

Traceback (most recent call last):
  File "/home/lee/Code/cas/venv/lib/python3.4/site-packages/rq/worker.py", line 568, in perform_job
    rv = job.perform()
  File "/home/lee/Code/cas/venv/lib/python3.4/site-packages/rq/job.py", line 

495, in perform
    self._result = self.func(*self.args, **self.kwargs)
  File "/home/lee/Code/cas/venv/lib/python3.4/site-packages/rq/job.py", line 206, in func
    return import_attribute(self.func_name)
  File "/home/lee/Code/cas/venv/lib/python3.4/site-packages/rq/utils.py", line 151, in import_attribute
    return getattr(module, attribute)
AttributeError: 'module' object has no attribute '_send_password_reset_email_async

'

添加評論,因為我也遇到了這個錯誤:

當您更改代碼時,Django 開發網絡服務器會動態重新加載,但 rqworker 進程不會。 因此,如果您添加/修改任務並立即調用它,您可能會遇到此錯誤,因為新代碼(您的 Web 代碼)嘗試調用舊代碼(RQ 任務)。

停止/重新啟動 rqworker 就可以了。

因此,您實際上需要做的是將模塊導入某個文件中,然后對其進行延遲。 @MrE,所以沒有必要在init .py 文件中導入它

如何將_send_password_reset_email_async包含在 reset_email 函數中 -

def send_password_reset_email(modeladmin, request, queryset):  
    def _send_password_reset_email_async(email):                             
        print(email)  

    for user in queryset:                                                
        django_rq.enqueue(_send_password_reset_email_async, user.email)  

send_password_reset_email.short_description = 'Send password reset email'

https://github.com/ui/django-rq/issues/125 中復制@richardbarran 的評論

當您更改代碼時,Rqworker 不會動態重新加載,但 Django 開發網絡服務器會因此如果您添加/修改任務並立即調用它,您可能會遇到此錯誤,因為新代碼(您的網絡代碼)試圖調用舊代碼代碼(RQ 任務)。

停止/重新啟動 rqworker 就可以了。

我有一個類似的問題,確保在應用程序的__init__.py中導入模塊解決了這個問題。

嘗試在另一個文件中編寫您的函數,將其作為模塊導入

# in my_utility.py
def send_password_reset_email_async(email):                             
    print(email)  

然后

import django_rq                                                         
from my_utility import send_password_reset_email_async

#def _send_password_reset_email_async(email):                             
#   print(email)                                                         

# Django admin action to send reset password emails                                                                 
def send_password_reset_email(modeladmin, request, queryset):            
    for user in queryset:                                                
        django_rq.enqueue(send_password_reset_email_async, user.email)  
send_password_reset_email.short_description = 'Send password reset email'                                                       

就我而言,我沒有將任務放在 tasks.py 文件中...

當您從主目錄或其他目錄運行 rqworker 時,會出現此問題。 在命令提示符中導航到您的 python 文件所在的目錄,然后運行 ​​rqworker 命令。

注意:注意你的 redis 服務器運行的端口,如果它不是默認的,即 6379 那么你將不得不在像這樣運行 rqworker 時提到該端口, your_python_file_directory_path $ rqworker --url redis://localhost:port

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM