Heroku 30s超时错误,如何让Django将变量创建视为后台任务(worker)



我的django网络应用程序面临heroku 30s超时错误。之所以花费很长时间是因为创建了视图变量(reddit api到上下文(

这是相同的代码。

def home(request):
reddit = praw.Reddit(client_id='myclientid', client_secret='mysecretcode',
user_agent='user agent name')
hot_posts = reddit.subreddit('AskReddit').top(time_filter="day", limit=7)
x = []
y = []

for post in hot_posts:
x.append(post.title)
y.append(post.url)
print(x)
print(y)
z = []
for url in y:
comments = []
submission = reddit.submission(url=url)
submission.comments.replace_more(limit=0)
for count in range(10):
comments.append(submission.comments[count].body)
z.append(comments)

top_EarthPorn = reddit.subreddit('EarthPorn').top(limit=100)
EarthPorn_links = []
for post in top_EarthPorn:
EarthPorn_links.append(post.url)
request.session['EarthPorn_links'] = EarthPorn_links
return render(request, template_name='base.html', context=context)

如何确保每小时左右创建一次上下文dict数据作为后台进程?可以使用哪些库来实现

我认为这应该有效:

把这个放在settings.py文件的末尾:

SESSION_EXPIRE_SECONDS = 1500 # 1500 seconds = 25 minutes

因此,会话将在25分钟后到期。

最新更新