为什么我的任务不会运行?当我这样做时,我得到了一个巨大的回溯

时间:2016-08-06 15:30:19

标签: python django redis queue scheduled-tasks

我不太明白任务是如何运作的。我从rq网站上的文档中运行了这个示例,它就像它应该的那样运行。那么我尝试使用我创建的函数。所以我像这样导入了它

我的task.py

import os

import redis
from rq import Worker, Queue, Connection
from src.blog.my_scraps import panties

import requests

listen = ['high', 'default', 'low']

redis_url = os.getenv('REDISTOGO_URL', 'redis://localhost:6379')

conn = redis.from_url(redis_url)

if __name__ == '__main__':
    with Connection(conn):
        worker = Worker(map(Queue, listen))
        worker.work()


panties()

我得到的是这个

(practice) apples-MBP:practice ray$ python my_raddqueue.py
Traceback (most recent call last):
  File "my_raddqueue.py", line 1, in <module>
    from my_task import conn, is_page_ok #count_words_at_url
  File "/Users/ray/Desktop/myheroku/practice/my_task.py", line 5, in <module>
    from src.blog.my_scraps import panties
  File "/Users/ray/Desktop/myheroku/practice/src/blog/my_scraps.py", line 3, in <module>
    from .models import Post
  File "/Users/ray/Desktop/myheroku/practice/src/blog/models.py", line 3, in <module>
    from taggit.managers import TaggableManager
  File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/taggit/managers.py", line 7, in <module>
    from django.contrib.contenttypes.models import ContentType
  File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/contrib/contenttypes/models.py", line 159, in <module>
    class ContentType(models.Model):
  File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/contrib/contenttypes/models.py", line 160, in ContentType
    app_label = models.CharField(max_length=100)
  File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/db/models/fields/__init__.py", line 1072, in __init__
    super(CharField, self).__init__(*args, **kwargs)
  File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/db/models/fields/__init__.py", line 166, in __init__
    self.db_tablespace = db_tablespace or settings.DEFAULT_INDEX_TABLESPACE
  File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/conf/__init__.py", line 55, in __getattr__
    self._setup(name)
  File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/conf/__init__.py", line 41, in _setup
    % (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

我是django的新手,甚至是python的新手,我昨天刚开始使用rq,因为我想自动化我的网站。我看到它说

You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

但我不知道该做什么。任何帮助,将不胜感激。感谢。

修改

这是我正在尝试使用的功能

 def p_panties():

        def swappo():
            user_one = ' "Mozilla/5.0 (Windows NT 6.0; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0" '
            user_two = ' "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5)" '
            user_thr = ' "Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko" '
            user_for = ' "Mozilla/5.0 (Macintosh; Intel Mac OS X x.y; rv:10.0) Gecko/20100101 Firefox/10.0" '

            agent_list = [user_one, user_two, user_thr, user_for]
            a = random.choice(agent_list)
            return a

        headers = {
            "user-agent": swappo(),
            "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
            "accept-charset": "ISO-8859-1,utf-8;q=0.7,*;q=0.3",
            "accept-encoding": "gzip,deflate,sdch",
            "accept-language": "en-US,en;q=0.8",
        }

        from lxml import html
        pan_url = 'http://www.panvideos.com'
        shtml = requests.get(pan_url, headers=headers)
        soup = BeautifulSoup(shtml.text, 'html5lib')
        video_row = soup.find_all('div', {'class': 'video'})
        name = 'pan videos'

        if os.getenv('_system_name') == 'OSX':
            author = User.objects.get(id=2)
        else:
            author = User.objects.get(id=3)

        def youtube_link(url):
            youtube_page = requests.get(url, headers=headers)
            soupdata = BeautifulSoup(youtube_page.text, 'html5lib')
            video_row = soupdata.find_all('script', {'type': 'text/javascript'})
            entries = [{'text': str(div),
                        } for div in video_row]
            tubby = str(entries[4])
            urls = re.findall('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', tubby)
            return urls

        def embed(url):
            new_embed = url.replace("watch?v=", "embed/")
            return new_embed

        entries = [{'href': div.a.get('href'),
                    'src': youtube_link(div.a.get('href'))[1],
                    'text': div.h4.text,
                    'comments': div.h4.text,
                    'name': name,
                    'url': div.a.get('href'),
                    'embed': embed(youtube_link(div.a.get('href'))[0]),
                    'author': author,
                    'video': True,
                    } for div in video_row][:13]

        for entry in entries:
            post = Post()
            post.title = entry['text']
            title = post.title
            if not Post.objects.filter(title=title):
                post.title = entry['text']
                post.name = entry['name']
                post.url = entry['url']
                post.body = entry['comments']
                post.image_url = entry['src']
                post.video_path = entry['embed']
                post.author = entry['author']
                post.video = entry['video']
                post.status = 'draft'
                post.save()
                post.tags.add("video", "Musica")

        return entries

修改

我试过

export DJANGO_SETTINGS_MODULE=gettingstarted.settings

并获得以下追溯

Traceback (most recent call last):
      File "my_raddqueue.py", line 1, in <module>
        from my_task import conn, is_page_ok #count_words_at_url
      File "/Users/ray/Desktop/myheroku/practice/my_task.py", line 6, in <module>
        from src.blog.my_scraps import panties
      File "/Users/ray/Desktop/myheroku/practice/src/blog/my_scraps.py", line 3, in <module>
        from .models import Post
      File "/Users/ray/Desktop/myheroku/practice/src/blog/models.py", line 3, in <module>
        from taggit.managers import TaggableManager
      File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/taggit/managers.py", line 7, in <module>
        from django.contrib.contenttypes.models import ContentType
      File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/contrib/contenttypes/models.py", line 159, in <module>
        class ContentType(models.Model):
      File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/contrib/contenttypes/models.py", line 160, in ContentType
        app_label = models.CharField(max_length=100)
      File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/db/models/fields/__init__.py", line 1072, in __init__
        super(CharField, self).__init__(*args, **kwargs)
      File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/db/models/fields/__init__.py", line 166, in __init__
        self.db_tablespace = db_tablespace or settings.DEFAULT_INDEX_TABLESPACE
      File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/conf/__init__.py", line 55, in __getattr__
        self._setup(name)
      File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/conf/__init__.py", line 43, in _setup
        self._wrapped = Settings(settings_module)
      File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/conf/__init__.py", line 99, in __init__
        mod = importlib.import_module(self.SETTINGS_MODULE)
      File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/importlib/__init__.py", line 126, in import_module
        return _bootstrap._gcd_import(name[level:], package, level)
    ImportError: No module named 'gettingstarted'

mysettings位于设置目录

 gettingstarted\
     __init__.py
     base.py
     local.py
     production.py

我也试过

DJANGO_SETTINGS_MODULE="gettingstarted.settings.base"

我也试过

export DJANGO_SETTINGS_MODULE="src.gettingstarted.settings.base"

得到了消息

        Traceback (most recent call last):
      File "my_raddqueue.py", line 1, in <module>
        from my_task import conn, is_page_ok #count_words_at_url
      File "/Users/ray/Desktop/myheroku/practice/my_task.py", line 6, in <module>
        from src.blog.my_scraps import panties
      File "/Users/ray/Desktop/myheroku/practice/src/blog/my_scraps.py", line 3, in <module>
        from .models import Post
      File "/Users/ray/Desktop/myheroku/practice/src/blog/models.py", line 3, in <module>
        from taggit.managers import TaggableManager
      File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/taggit/managers.py", line 7, in <module>
        from django.contrib.contenttypes.models import ContentType
      File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/contrib/contenttypes/models.py", line 159, in <module>
        class ContentType(models.Model):
      File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/db/models/base.py", line 94, in __new__
        app_config = apps.get_containing_app_config(module)
      File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/apps/registry.py", line 239, in get_containing_app_config
        self.check_apps_ready()
      File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/apps/registry.py", line 124, in check_apps_ready
        raise AppRegistryNotReady("Apps aren't loaded yet.")
    django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

修改

这是我的wsgi.py

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "gettingstarted.settings")

from django.core.wsgi import get_wsgi_application
from whitenoise.django import DjangoWhiteNoise

application = get_wsgi_application()
application = DjangoWhiteNoise(application)

这是我的设置/ base.py

的片段
INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    # local
    'blog',

    # Third party
    'crispy_forms',
    'taggit',
    'haystack',
    'whoosh',
    'pagedown',
    'markdown_deux'
)

修改

尝试将此添加到my_task.py

import django
from django.conf import settings
from src.gettingstarted.settings import base

settings.configure(default_settings=base, DEBUG=True)
django.setup()

我收到了一条新的错误消息

File "my_raddqueue.py", line 1, in <module>
        from my_task import conn, count_words_at_url, lett, p_panties # is_page_ok
      File "/Users/ray/Desktop/myheroku/practice/my_task.py", line 13, in <module>
        django.setup()
      File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/__init__.py", line 17, in setup
        configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
      File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/conf/__init__.py", line 56, in __getattr__
        return getattr(self._wrapped, name)
      File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/conf/__init__.py", line 173, in __getattr__
        return getattr(self.default_settings, name)
    AttributeError: module 'src.gettingstarted.settings.base' has no attribute 'LOGGING_CONFIG'

修改

我添加了

LOGGING_CONFIG = None

到我的base.py因为这条消息

module 'src.gettingstarted.settings.base' has no attribute 'LOGGING_CONFIG'

然后我添加了一条新消息

module 'src.gettingstarted.settings.base' has no attribute 'LOGGING'

所以我添加了

LOGGING = None

然后我收到了这条消息

 No module named 'blog'

我正在尝试另一种方法,因为这会变得很烦人。我认为问题正在发生,因为我的功能刮擦并将数据添加到我的模型中,我认为需要进行身份验证。

这两个进口

from django.contrib.auth.models import User
from src.blog.models import Post

导致回溯消息

        File "my_raddqueue.py", line 1, in <module>
        from my_task import conn, count_words_at_url, lett # is_page_ok
      File "/Users/ray/Desktop/myheroku/practice/my_task.py", line 10, in <module>
        from django.contrib.auth.models import User
      File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/contrib/auth/models.py", line 4, in <module>
        from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
      File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/contrib/auth/base_user.py", line 49, in <module>
        class AbstractBaseUser(models.Model):
      File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/contrib/auth/base_user.py", line 50, in AbstractBaseUser
        password = models.CharField(_('password'), max_length=128)
      File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/db/models/fields/__init__.py", line 1072, in __init__
        super(CharField, self).__init__(*args, **kwargs)
      File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/db/models/fields/__init__.py", line 166, in __init__
        self.db_tablespace = db_tablespace or settings.DEFAULT_INDEX_TABLESPACE
      File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/conf/__init__.py", line 55, in __getattr__
        self._setup(name)
      File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/conf/__init__.py", line 43, in _setup
        self._wrapped = Settings(settings_module)
      File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/conf/__init__.py", line 99, in __init__
        mod = importlib.import_module(self.SETTINGS_MODULE)
      File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/importlib/__init__.py", line 126, in import_module
        return _bootstrap._gcd_import(name[level:], package, level)
    ImportError: No module named 'gettingstarted'

0 个答案:

没有答案