开发者社区> 问答> 正文

Heroku Postgres + Python:连接太多错误

我试图建立的应用程序,得到保存的用户时间轴上的推文在烧瓶。但是出现了这个错误。 这个程序在我的本地服务器/环境中运行,但是当我在Heroku上托管它时,它就崩溃了。 app / init . py

# Initialized app
app = Flask(__name__, instance_relative_config=True)
configure_app(app)

# Initialized database
db = SQLAlchemy(app)
migrate = Migrate(app, db)
db.init_app(app)

# Get timeline tweet
import main.get_tweet

app / get_tweet.py

from flask import current_app
from requests_oauthlib import OAuth1Session
from apscheduler.schedulers.background import BackgroundScheduler

from main.database import save_tweet

import tweepy

def get_current_tweet():
    client_id = current_app.config['TWITTER_CLIENT_ID']
    client_secret = current_app.config['TWITTER_CLIENT_SECRET']

    base_url = 'https://api.twitter.com/'
    user_timeline_url = base_url + 'statuses/user_timeline'

    users = fetch_users()    
    for user in users:
        try:
            twitter_profile = user.twitter_profile_info()

            oauth_token = twitter_profile.access_token
            oauth_verifier = twitter_profile.access_secret

            auth = tweepy.OAuthHandler(client_id, client_secret)
            auth.set_access_token(oauth_token, oauth_verifier)

            api = tweepy.API(auth)
            user_tweets=api.user_timeline(screen_name=twitter_profile.screen_name, count=1, exclude_replies=False, include_rts=False)

            status = user_tweets[0]._json
            save_tweet(status, user)
        except:
            print(user.name + " has been errored.")

sched = BackgroundScheduler(standalone=True,coalesce=True)
sched.add_job(get_current_tweet, 'interval', minutes=10)
sched.start()

app / database.py

def update_or_create_current_tweet(request, user):
    twitter_profile = user.twitter_profile_info()
    uid = request['id']
    url =  "https://twitter.com/" + twitter_profile.screen_name + "/status/" + str(uid)
    content = request['text']
    tweet = Tweet(
       uid=uid,
       user_id=user.id,
       url=url,
       content=content
    )
    db.session.add(tweet)
    db.session.commit()
    return current_tweet

问题来源StackOverflow 地址:/questions/59385821/heroku-postgres-python-too-many-connections-error

展开
收起
kun坤 2019-12-25 22:13:11 402 0
0 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
From Python Scikit-Learn to Sc 立即下载
Data Pre-Processing in Python: 立即下载
双剑合璧-Python和大数据计算平台的结合 立即下载