gpt4 book ai didi

python - 我的 celery redis 任务在 heroku 服务器上的 django 应用程序中不起作用

转载 作者:可可西里 更新时间:2023-11-01 11:20:02 27 4
gpt4 key购买 nike

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

celery worker -A blog -l info. 

所以我猜问题可能与此有关。因为我不知道该怎么做。我怀疑我是否应该在我的应用程序中这样做。这是我的代码

celery .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

这就是我在上面看到的环境变量中使用的内容。我怎样才能使它工作?

最佳答案

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

worker:celery worker -A blog -l info

关于python - 我的 celery redis 任务在 heroku 服务器上的 django 应用程序中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39551927/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com