【问题标题】:How do I run my Flask app remotely using MongoHQ and Heroku (Python)如何使用 MongoHQ 和 Heroku (Python) 远程运行我的 Flask 应用程序
【发布时间】:2013-08-07 10:37:10
【问题描述】:

我有一个用 Python 编写的脚本,可以让我将来自终端的推文消费到本地托管的 mongodb 数据库中。为了提高正常运行时间,我想在 Heroku 上远程托管这个脚本,并将消费的推文拍摄到由 MongoHQ 托管的数据库中。因为我想在不使用 Django 的情况下执行此操作,所以我使用 Flask 框架将应用程序部署到 Heroku(在此处描述:https://devcenter.heroku.com/articles/python)。

当我使用此设置运行一个简单的“hello world”应用程序时,一切都很好。但是,当我尝试运行我的推文消费应用程序时,它会立即崩溃。如何将我的应用程序更改为可以与 Flask/Heroku/MongoHQ 设置一起使用?源代码为:

import json
import pymongo
import tweepy

consumer_key = ""
consumer_secret = ""
access_key = ""
access_secret = ""

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)


class CustomStreamListener(tweepy.StreamListener):
    def __init__(self, api):
        self.api = api
        super(tweepy.StreamListener, self).__init__()

        self.db = pymongo.MongoClient().test

    def on_data(self, tweet):
        self.db.tweets.insert(json.loads(tweet))

    def on_error(self, status_code):
        return True # Don't kill the stream

    def on_timeout(self):
        return True # Don't kill the stream


sapi = tweepy.streaming.Stream(auth, CustomStreamListener(api))
sapi.filter(track=['rooney'])

我对编程完全陌生,所以我想这个问题的解决方案可能很简单。但是,我被困住了,真的可以使用一些帮助来取得进步。

【问题讨论】:

  • 我的 heroku 日志是:State changed from started to crashed 2013-08-06T10:07:44.409065+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed在启动后 60 秒内绑定到 $PORT 2013-08-06T10:07:44.409351+00:00 heroku[web.1]: 使用 SIGKILL 停止进程

标签: python heroku flask tweepy mongohq


【解决方案1】:

没有更多信息很难调试,但我的第一个猜测是你没有安装依赖项。

Heroku 为您提供了一个干净的 Python 环境。但是,您需要像 tweepy 这样默认不安装的特殊库。因此,您必须让 Heroku 知道安装这些。

您需要使用 pip 和 requirements.txt 文档,其中列出了您尝试使用的所有库以及版本号。

https://devcenter.heroku.com/articles/python-pip

【讨论】:

猜你喜欢
  • 2015-01-05
  • 2020-07-15
  • 1970-01-01
  • 2020-02-28
  • 2011-07-14
  • 2013-02-01
  • 1970-01-01
  • 2021-11-03
  • 1970-01-01
相关资源
最近更新 更多