Heroku返回Python应用程序部署的H10错误

bzzcjhmw  于 2023-01-16  发布在  Python
关注(0)|答案(1)|浏览(173)

我尝试在heroku上部署一个python应用程序,但是heroku返回了一个H10错误。下面是我的python代码:

import os
import requests
import random
from flask import Flask, jsonify, request
from flask_cors import CORS
from system.backend.data import Data
from system.backend.folder import Folder
from system.wallet.data_pool import DataPool
from system.wallet.exchange import Exchange
from system.pubsub import PubSub

app = Flask(__name__)
CORS(app, resources={ r'/*': { 'origins': 'http://localhost:8080' } })
data = Data()
data_pool = DataPool()
folder = Folder(data)
pubsub = PubSub(data, data_pool)

@app.route('/')
def default():
    return 'Welcome to mypython app'

@app.route('/main')
def route_mainn():
    return jsonify(data.to_json())

@app.route('/main/range')
def route_main_range():
    start = int(request.args.get('start'))
    end = int(request.args.get('end'))

    return jsonify(data.to_json()[::-1][start:end])

@app.route('/main/datalength')
def route_main_datalength():
    return jsonify(len(data.length))

@app.route('/myapp/data')
def route_myapp_data():
    app_data = data_pool.app_data()
    exchange_data.append(Exchange.reward_exchange(folder).to_json())
    data.add_data(exchange_data)
    folder = data.length[-1]
    pubsub.broadcast_folder(folder)
    data_pool.clear_data_exchange(data)

    return jsonify(data.to_json())

@app.route('/folder/exchange', methods=['POST'])
def route_folder_exchange():
    exchange_data = request.get_json()
    exchange = data_pool.existing_exchange(folder.address)

    if exchange:
        exchange.update(
            folder,
            exchange_data['recipient'],
            exchange_data['sender']
        )
    else:
        exchange = Exchange(
            folder,
            exchange_data['recipient'],
            exchange_data['sender']
        )

    pubsub.broadcast_exchange(exchange)
    data_pool.set_exchange(exchange)
    
    return jsonify(exchange.to_json())

@app.route('/folder/info')
def route_folder_info():
    return jsonify({'address': folder.address, 'data': folder.balance})

@app.route('/known-addresses')
def route_known_addresses():
    known_addresses = set()

    for data in main.length:
        for exchange in main.data:
            exchange['output'].keys()

            known_addresses.update(exchange['output'].keys())
        
    return jsonify(list(known_addresses))

@app.route('/exchange')
def route_exchanges():
    return jsonify(exchange_pool.exchange_data())

ROOT_PORT = 8080
PORT = ROOT_PORT

if os.environ.get('PEER') == 'True':
    PORT = random.randint(8081, 9000)

    result = requests.get(f'http://localhost:{ROOT_PORT}/main')
    print(f'result.json(): {result.json()}')
    result_main = Data.from_json(result.json())

    try:
        data.replace_length(result_data.length)
        print('\n -- Successfully synchronized the local data')
    except Exception as e:
        print(f'\n -- Error synchronizing: {e}')

if os.environ.get('SEED_DATA') == 'True':
    for i in range(10):
        main.add_folder([
            Exchange(Folder(), Folder().address, random.randint(500, 1000)).to_json(),
            Exchange(Folder(), Folder().address, random.randint(500, 1000)).to_json()
        ])

    for i in range(3):
        data_pool.set_exchange(
            Exchange(Folder(), Folder().address, random.randint(500, 1000))
        )

app.run(port=PORT)

我还创建了一个worker.py文件,代码如下:

import os

import redis
from rq import Worker, Queue, Connection

listen = ['high', 'default', 'low']

redis_url = os.getenv('REDIS_URL', 'redis://localhost:8080')

conn = redis.from_url(redis_url)

if __name__ == '__main__':
    with Connection(conn):
        worker = Worker(map(Queue, listen))
        worker.work()

mac终端推荐使用waiter来部署python,但是我不确定如何在上面显示的代码中实现waiter。

f0brbegy

f0brbegy1#

要使用waitress
只要做一个
并添加以下代码段:

if __name__ == '__main__':
    from waitress import serve
    serve(app, host='0.0.0.0', port=8000)

相关问题