简体   繁体   English

在heroku上运行python Web服务

[英]Running a python web service on heroku

I have a Python-flask web service which is running absolutely fine on my localhost server. 我有一个Python烧瓶式Web服务,该服务在我的本地主机服务器上运行得非常好。 Now I have deployed it on heroku and it doesn't run when I try to access it from heroku showing me that application has some errors. 现在,我已将其部署在heroku上,当我尝试从heroku访问它时,它没有运行,这表明该应用程序有一些错误。 I tested it on localhost(and confirmed that the web service runs absolutely fine) and then deployed it on heroku. 我在localhost上对其进行了测试(并确认Web服务运行完全正常),然后将其部署在heroku上。 I am using a shelve file from which my web service retrieves the persistent data. 我正在使用一个搁置文件,我的Web服务从该文件中检索持久性数据。 When it runs on localhost it runs fine but I think at heroku it fails to retrieve the contents of my shelve file. 当它在localhost上运行时,它运行良好,但我认为在heroku上,它无法检索到我的文件中的内容。 In my earlier web service that I had deployed on Heroku I used to create new shelve file on every run but in this web service I have already created shelve file with persistent data loaded in it and my web service just refers to this data without writing anything to it. 在我以前部署在Heroku上的早期Web服务中,我每次运行时都会创建一个新的Shelve文件,但是在此Web Service中,我已经创建了一个加载了永久数据的Shelve文件,而我的Web Service只是引用此数据而无需编写任何内容对它。

Here is my web service script: 这是我的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})

EDIT 编辑

This is my log 这是我的日志

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

As discussed over chat 如通过聊天讨论

  • All files you are accessing in code are in git 您正在代码中访问的所有文件都在git中
  • You are not modifying any file from your code 您没有从代码中修改任何文件
  • Getting 500 error when running the code 运行代码时出现500错误

In my opinion you should check your code, it might be generating some exception which are unhandled right now. 我认为您应该检查您的代码,它可能会生成一些目前未处理的异常。 What you can do is to wrap your code inside try-except block and print the exception. 您可以做的是将代码包装在try-except块中并打印异常。 like- 喜欢-

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)})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM