【问题标题】:My celery redis task isn't working in my django app on heroku server我的 celery redis 任务在 heroku 服务器上的 django 应用程序中不起作用
【发布时间】:2017-01-25 21:20:36
【问题描述】:

我有一个任务在我的本地服务器上运行良好,但是当我将它推送到 Heroku 时,没有任何反应。没有错误消息。在这方面我是新手,在本地我会通过做

celery worker -A blog -l info. 

所以我猜这可能与此有关。因为我不知道这样做。我怀疑我应该在我的应用程序中执行此操作。这是我的代码

芹菜.py

import os

from celery import Celery

from django.conf import settings

# set the default Django settings module for the 'celery' program.
os.environ.setdefault(
    'DJANGO_SETTINGS_MODULE', 'gettingstarted.settings'
)

app = Celery('blog')

# Using a string here means the worker will not have to
# pickle the object when using Windows.
app.config_from_object('django.conf:settings')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)

我的任务.py

import requests
import random
import os

from bs4 import BeautifulSoup
from .celery import app
from .models import Post
from django.contrib.auth.models import User


@app.task
def the_star():
    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",
    }

    # scraping from worldstar
    url_to = 'http://www.worldstarhiphop.com'
    html = requests.get(url_to, headers=headers)
    soup = BeautifulSoup(html.text, 'html5lib')
    titles = soup.find_all('section', 'box')
    name = 'World Star'

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

    def make_soup(url):
        the_comments_page = requests.get(url, headers=headers)
        soupdata = BeautifulSoup(the_comments_page.text, 'html5lib')
        comment = soupdata.find('div')
        para = comment.find_all('p')
        kids = [child.text for child in para]
        blu = str(kids).strip('[]')
        return blu

    cleaned_titles = [title for title in titles if title.a.get('href') != 'vsubmit.php']
    world_entries = [{'href': url_to + box.a.get('href'),
                      'src': box.img.get('src'),
                      'text': box.strong.a.text,
                      'comments': make_soup(url_to + box.a.get('href')),
                      'name': name,
                      'url': url_to + box.a.get('href'),
                      'embed': None,
                      'author': None,
                      'video': False
                      } for box in cleaned_titles][:10] # The count

    for entry in world_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 world_entries

我的意见.py

def shopan(request):
    the_star.delay()
    return redirect('/')

我有多个 REDIS_URL 实例

所以我跑了

heroku redis:promote REDIS_URL

这就是我在我的环境变量中使用的,你可以在上面看到。我怎样才能做到这一点?

【问题讨论】:

    标签: python django heroku deployment redis


    【解决方案1】:

    你需要在你的 Procfile 中添加一个条目来告诉 Heroku 启动 Celery worker:

    worker:celery worker -A blog -l info
    

    【讨论】:

    • 我只是按照你说的做了,然后把它推送到我的服务器上,但什么也没发生。我回到文档并搜索 procfile,它说 heroku ps:scale worker=1,我试图这样做,它说找不到那个形成
    • 我不得不把它隔开,这样冒号和芹菜之间就有了空间,就像这个工人一样: celery worker -A blog -l info 然后我必须运行 heroku ps:scale worker=1 和 ot工作
    猜你喜欢
    • 2020-04-29
    • 2018-09-17
    • 1970-01-01
    • 2012-12-20
    • 2012-01-22
    • 2020-09-07
    • 2017-03-19
    • 2017-10-22
    • 2020-12-20
    相关资源
    最近更新 更多