在heroku上运行python Web服务

时间:2014-03-19 07:22:10

标签: python heroku shelve

我有一个Python-flask Web服务,它在我的localhost服务器上运行得非常好。现在我已经将它部署在heroku上,当我尝试从heroku访问它时它不会运行,这表明应用程序有一些错误。我在localhost上测试了它(并确认Web服务运行得非常好),然后将其部署在heroku上。我正在使用搁置文件,我的Web服务从中检索持久数据。当它在localhost上运行时它运行正常但我认为在heroku它无法检索我的shelve文件的内容。在我之前在Heroku上部署的Web服务中,我曾经在每次运行时都创建了新的搁置文件但是在这个Web服务中我已经创建了搁置文件,其中加载了持久性数据,而我的Web服务只是引用了这些数据而没有写任何内容它。

这是我的网络服务脚本:

news_index=shelve.open('IndexedMapping')

item = [] # list for storing the final results

with open('TodaysToiScrapedItemsOutput.json') as f: #load json file
    data = json.load(f)

input_headline = news_string
input_headline_list =  input_headline.split()
temp_input_headline_list = []

for each_word in input_headline_list:
    temp_input_headline_list.append(each_word)


for each_word in temp_input_headline_list:
    if (each_word.lower() in ignore_this_words):
        input_headline_list.remove(each_word)

hit_cnt=0
key_and_hit_cnt_dict={}
for each_key in news_index:
    for each_word in input_headline_list:
    if(each_word.lower() in each_key):
        hit_cnt = hit_cnt + 1   
    key_and_hit_cnt_dict[each_key] = hit_cnt
    hit_cnt=0

sorted_keys_wrt_hitcnt = sorted(key_and_hit_cnt_dict, key= key_and_hit_cnt_dict.__getitem__,reverse=True)


i=0
for each_entry in sorted_keys_wrt_hitcnt:
    if(i<5):
        location=news_index[each_entry]
        item.append({ 'body' : data[location]["body"],'location':location,'key':each_entry,'words':input_headline_list})
    i = i+1   

return jsonify({'item':item})

修改

这是我的日志

2014-03-19T10:09:08.000898+00:00 app[web.1]: 2014-03-19 10:09:07 [7] [INFO] Booting worker with pid: 7
2014-03-19T10:09:08.262376+00:00 heroku[web.1]: State changed from starting to up
2014-03-19T10:09:18.149027+00:00 heroku[router]: at=info method=GET path=/ host=glacial-  plateau-3427.herokuapp.com request_id=203107b5-5c0e-40bd-8e0b-4cdc8649c2f1 fwd="27.251.95.162" dyno=web.1 connect=2ms service=25ms status=404 bytes=384
2014-03-19T10:09:24.995531+00:00 heroku[router]: at=info method=GET path=/toi/cricket host=glacial-plateau-3427.herokuapp.com request_id=18d612f6-7cf6-4fc0-a686-6a8680cf469f fwd="27.251.95.162" dyno=web.1 connect=2ms service=18ms status=500 bytes=454
2014-03-19T10:10:45.866027+00:00 heroku[router]: at=info method=GET path=/toi/cricket%20India%20T20 host=glacial-plateau-3427.herokuapp.com request_id=5122179a-dfde-4a22-b916-daa7eec3ec10 fwd="27.251.95.162" dyno=web.1 connect=1ms service=6ms status=500 bytes=454
2014-03-19T10:13:39.713629+00:00 heroku[router]: at=info method=GET path=/toi/aap%20modi%20kejriwal host=glacial-plateau-3427.herokuapp.com request_id=0426e03c-61bd-4b4f-995b-55a72c91d676 fwd="27.251.95.162" dyno=web.1 connect=1ms service=5ms status=500 bytes=454

1 个答案:

答案 0 :(得分:1)

正如聊天所讨论的

  • 您在代码中访问的所有文件都是git
  • 您没有修改代码中的任何文件
  • 运行代码时出现500错误

在我看来,你应该检查你的代码,它可能会产生一些现在未处理的异常。你可以做的是将你的代码包装在try-except块中并打印异常。像 -

try:
    news_index=shelve.open('IndexedMapping')

    item = [] # list for storing the final results

    with open('TodaysToiScrapedItemsOutput.json') as f: #load json file
        data = json.load(f)

    input_headline = news_string
    input_headline_list =  input_headline.split()
    temp_input_headline_list = []

    for each_word in input_headline_list:
        temp_input_headline_list.append(each_word)


    for each_word in temp_input_headline_list:
        if (each_word.lower() in ignore_this_words):
            input_headline_list.remove(each_word)

    hit_cnt=0
    key_and_hit_cnt_dict={}
    for each_key in news_index:
        for each_word in input_headline_list:
            if(each_word.lower() in each_key):
                hit_cnt = hit_cnt + 1   
            key_and_hit_cnt_dict[each_key] = hit_cnt
        hit_cnt=0

    sorted_keys_wrt_hitcnt = sorted(key_and_hit_cnt_dict, key= key_and_hit_cnt_dict.__getitem__,reverse=True)


    i=0
    for each_entry in sorted_keys_wrt_hitcnt:
        if(i<5):
            location=news_index[each_entry]
            item.append({ 'body' : data[location]["body"],'location':location,'key':each_entry,'words':input_headline_list})
        i = i+1

    return jsonify({'item':item})
except Exception, e:
    import traceback
    traceback.print_exc()
    return jsonify({'error': str(e)})