使用Flask应用程序处理请求线程错误?

11

这可能有点冒险,但这是我收到的错误消息:

  File "/home/MY NAME/anaconda/lib/python2.7/SocketServer.py", line 596, in process_request_thread
    self.finish_request(request, client_address)
  File "/home/MY NAME/anaconda/lib/python2.7/SocketServer.py", line 331, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "/home/MY NAME/anaconda/lib/python2.7/SocketServer.py", line 654, in __init__
    self.finish()
  File "/home/MY NAME/anaconda/lib/python2.7/SocketServer.py", line 713, in finish
    self.wfile.close()
  File "/home/MY NAME/anaconda/lib/python2.7/socket.py", line 283, in close
    self.flush()
  File "/home/MY NAME/anaconda/lib/python2.7/socket.py", line 307, in flush
    self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 32] Broken pipe
我构建了一个 Flask 应用程序,它以地址作为输入并执行一些字符串格式化、操作等操作,然后通过 geopy 外部模块将它们发送到 Bing 地图进行地理编码。
我使用这个应用程序来清理非常大的数据集。该应用程序通常适用于 ~1,500 个地址(每行输入一个)。也就是说,它将处理地址并将其发送到 Bing 地图进行地理编码,然后返回结果。在处理约 1,500 个地址后,应用程序会变得没有响应。如果这种情况发生在工作时,代理会告诉我有一个 TCP 错误。如果我在非工作电脑上运行,则页面不会加载。如果我重新启动应用程序,那么它就可以正常运行。因此,我被迫使用每批约 1,000 个地址的程序运行(只是为了安全起见,因为我还不确定程序崩溃的确切数量)。
有谁知道可能是什么原因造成的吗?
我认为我可能已经达到了我的 Bing API 键限制(每天为 30,000),但这可能不准确,因为我很少使用超过 15,000 次的请求。
我第二个想法是,也许是因为我仍在使用标准的 Flask 服务器来运行我的应用程序。切换到 gunicorn 或 uWSGI 是否可以解决这个问题?
我第三个想法是它可能因为请求数量过多而超载。我尝试在处理第一个 1,000 个地址后将程序睡眠 15 秒左右,但这没有解决任何问题。
如果需要进一步的澄清,请让我知道。
以下是我 Flask 应用程序的后端代码。我从这个函数中获取输入:
@app.route("/clean", methods=['POST'])
def dothing():
    addresses = request.form['addresses']
    return cleanAddress(addresses)

这里是cleanAddress函数:目前有些混乱,因为有很多if语句来检查地址中特定的错别字,但我计划将很多代码移入到另一个文件中的其他函数中,并只通过这些函数传递地址以使其更加简洁。

def cleanAddress(addresses):

    counter = 0

    # nested helper function to fix addresses such as '30 w 60th'
    def check_st(address):
        if 'broadway' in address:
            return address
        has_th_st_nd_rd = re.compile(r'(?P<number>[\d]{1,4}(th|st|nd|rd)\s)(?P<following>.*)')
        has_number = has_th_st_nd_rd.search(address)
        if has_number is not None:
            if re.match(r'(street|st|floor)', has_number.group('following')):   
                return address
            else:
                new_address = re.sub('(?P<number>[\d]{1,4}(st|nd|rd|th)\s)', r'\g<number>street ', address, 1)
                return new_address
        else:
            return address

    addresses = addresses.split('\n')
    cleaned = []
    success = 0
    fail = 0
    cleaned.append('<body bgcolor="#FACC2E"><center><img src="http://goglobal.dhl-usa.com/common/img/dhl-express-logo.png" alt="Smiley face" height="100" width="350"><br><p>')

    cleaned.append('<br><h3>Note: Everything before the first comma is the Old Address. Everything after the first comma is the New Address</h13>')
    cleaned.append('<p><h3>To format the output in Excel, split the columns using "," as the delimiter. </p></h3>')
    cleaned.append('<p><h2><font color="red">Old Address </font> <font color="black">New Address </font></p></h2>')

    for address in addresses:
        dirty = address.strip()
        if ',' in address:
            dirty = dirty.replace(',', '')
        cleaned.append('<font color="red">' + dirty + ', ' + '</font>')

        address = address.lower()
        address = re.sub('[^A-Za-z0-9#]+', ' ', address).lstrip()

        pattern = r"\d+.* +(\d+ .*(" + "|".join(patterns) + "))"
        address = re.sub(pattern, "\\1", address)

        address = check_st(address) 


        if 'one ' in address:
            address = address.replace('one', '1')
        if 'two' in address:
            address = address.replace('two', '2')
        if 'three' in address:
            address = address.replace('three', '3')
        if 'four' in address:
            address = address.replace('four', '4')
        if 'five' in address:
            address = address.replace('five', '5')
        if 'eight' in address:
            address = address.replace('eight', '8')
        if 'nine' in address:
            address = address.replace('nine', '9')
        if 'fith' in address:
            address = address.replace('fith', 'fifth')
        if 'aveneu' in address:
            address = address.replace('aveneu', 'avenue')
        if 'united states of america' in address:
            address = address.replace('united states of america', '')
        if 'ave americas' in address:
            address = address.replace('ave americas', 'avenue of the americas')
        if 'americas avenue' in address:
            address = address.replace('americas avenue', 'avenue of the americas')
        if 'avenue of americas' in address:
            address = address.replace('avenue of americas', 'avenue of the americas')
        if 'avenue of america ' in address:
            address = address.replace('avenue of america ', 'avenue of the americas ')
        if 'ave of the americ' in address:
            address = address.replace('ave of the americ', 'avenue of the americas')
        if 'avenue america' in address:
            address = address.replace('avenue america', 'avenue of the americas')
        if 'americaz' in address:
            address = address.replace('americaz', 'americas')
        if 'ave of america' in address:
            address = address.replace('ave of america', 'avenue of the americas')
        if 'amrica' in address:
            address = address.replace('amrica', 'americas')
        if 'americans' in address:
            address = address.replace('americans', 'americas')
        if 'walk street' in address:
            address = address.replace('walk street', 'wall street')
        if 'northend' in address:
            address = address.replace('northend', 'north end')
        if 'inth' in address:
            address = address.replace('inth', 'ninth')
        if 'aprk' in address:
            address = address.replace('aprk', 'park')
        if 'eleven' in address:
            address = address.replace('eleven', '11')
        if ' av ' in address:
            address = address.replace(' av ', ' avenue')
        if 'avnue' in address:
            address = address.replace('avnue', 'avenue')
        if 'ofthe americas' in address:
            address = address.replace('ofthe americas', 'of the americas')
        if 'aj the' in address:
            address = address.replace('aj the', 'of the')
        if 'fifht' in address:
            address = address.replace('fifht', 'fifth')
        if 'w46' in address:
            address = address.replace('w46', 'w 46')
        if 'w42' in address:
            address = address.replace('w42', 'w 42')
        if '95st' in address:
            address = address.replace('95st', '95th st')
        if 'e61 st' in address:
            address = address.replace('e61 st', 'e 61st')
        if 'driver information' in address:
            address = address.replace('driver information', '')
        if 'e87' in address:
            address = address.replace('e87', 'e 87')
        if 'thrd avenus' in address:
            address = address.replace('thrd avenus', 'third avenue')
        if '3r ' in address:
            address = address.replace('3r ', '3rd ')
        if 'st ates' in address:
            address = address.replace('st ates', '')
        if 'east52nd' in address:
            address = address.replace('east52nd', 'east 52nd')
        if 'authority to leave' in address:
            address = address.replace('authority to leave', '')
        if 'sreet' in address:
            address = address.replace('sreet', 'street')
        if 'w47' in address:
            address = address.replace('w47', 'w 47')
        if 'signature required' in address:
            address = address.replace('signature required', '')
        if 'direct' in address:
            address = address.replace('direct', '')
        if 'streetapr' in address:
            address = address.replace('streetapr', 'street')
        if 'steet' in address:
            address = address.replace('steet', 'street')
        if 'w39' in address:
            address = address.replace('w39', 'w 39')
        if 'ave of new york' in address:
            address = address.replace('ave of new york', 'avenue of the americas')
        if 'avenue of new york' in address:
            address = address.replace('avenue of new york', 'avenue of the americas')
        if 'brodway' in address:
            address = address.replace('brodway', 'broadway')
        if 'w 31 ' in address:
            address = address.replace('w 31 ', 'w 31th ')
        if 'w 34 ' in address:
            address = address.replace('w 34 ', 'w 34th ')
        if 'w38' in address:
            address = address.replace('w38', 'w 38')
        if 'broadeay' in address:
            address = address.replace('broadeay', 'broadway')
        if 'w37' in address:
            address = address.replace('w37', 'w 37')
        if '35street' in address:
            address = address.replace('35street', '35th street')
        if 'eighth avenue' in address:
            address = address.replace('eighth avenue', '8th avenue')
        if 'west 33' in address:
            address = address.replace('west 33', 'west 33rd')
        if '34t ' in address:
            address = address.replace('34t ', '34th ')
        if 'street ave' in address:
            address = address.replace('street ave', 'ave')
        if 'avenue of york' in address:
            address = address.replace('avenue of york', 'avenue of the americas')
        if 'avenue aj new york' in address:
            address = address.replace('avenue aj new york', 'avenue of the americas')
        if 'avenue ofthe new york' in address:
            address = address.replace('avenue ofthe new york', 'avenue of the americas')
        if 'e4' in address:
            address = address.replace('e4', 'e 4')
        if 'avenue of nueva york' in address:
            address = address.replace('avenue of nueva york', 'avenue of the americas')
        if 'avenue of new york' in address:
            address = address.replace('avenue of new york', 'avenue of the americas')
        if 'west end new york' in address:
            address = address.replace('west end new york', 'west end avenue')

        #print address    
        address = address.split(' ')
        for pattern in patterns:
            try:
                if address[0].isdigit():
                    continue
                else:
                    location = address.index(pattern) + 1
                    number_location = address[location]
                    #print address[location]
                    #if 'th' in address[location + 1] or 'floor' in address[location + 1] or '#' in address[location]:
                    #    continue
            except (ValueError, IndexError):
                continue
            if number_location.isdigit() and len(number_location) <= 4:
                address = [number_location] + address[:location] + address[location+1:]
                break
        address = ' '.join(address)

        if '#' in address:
            address = address.replace('#', '')


        #print (address)


        i = 0
        for char in address:
            if char.isdigit():
                address = address[i:]
                break
            i += 1


        #print (address)

        if 'plz' in address:
            address = address.replace('plz', 'plaza ', 1)
        if 'hstreet' in address:
            address = address.replace('hstreet', 'h street')
        if 'dstreet' in address:
            address = address.replace('dstreet', 'd street')
        if 'hst' in address:
            address = address.replace('hst', 'h st')
        if 'dst' in address:
            address = address.replace('dst', 'd st')
        if 'have' in address:
            address = address.replace('have', 'h ave')
        if 'dave' in address:
            address = address.replace('dave', 'd ave')
        if 'havenue' in address:
            address = address.replace('havenue', 'h avenue')
        if 'davenue' in address:
            address = address.replace('davenue', 'd avenue')



        #print address

        regex = r'(.*)(' + '|'.join(patterns) + r')(.*)'
        address = re.sub(regex, r'\1\2', address).lstrip() + " nyc"

        print (address)

        if 'americasas st' in address:
            address = address.replace('americasas st', 'americas')

        try:

            clean = geolocator.geocode(address)
            x = clean.address
            address, city, zipcode, country = x.split(",")
            address = address.lower()
            if 'first' in address:
                address = address.replace('first', '1st')
            if 'second' in address:
                address = address.replace('second', '2nd')
            if 'third' in address:
                address = address.replace('third', '3rd')
            if 'fourth' in address:
                address = address.replace('fourth', '4th')
            if 'fifth' in address:
                address = address.replace('fifth', '5th')
            if ' sixth a' in address:
                address = address.replace('ave', '')
                address = address.replace('avenue', '')
                address = address.replace(' sixth', ' avenue of the americas')
            if ' 6th a' in address:
                address = address.replace('ave', '')
                address = address.replace('avenue', '')
                address = address.replace(' 6th', ' avenue of the americas')
            if 'seventh' in address:
                address = address.replace('seventh', '7th')
            if 'fashion' in address:
                address = address.replace('fashion', '7th')
            if 'eighth' in address:
                address = address.replace('eighth', '8th')
            if 'ninth' in address:
                address = address.replace('ninth', '9th')
            if 'tenth' in address:
                address = address.replace('tenth', '10th')
            if 'eleventh' in address:
                address = address.replace('eleventh', '11th')


            zipcode = zipcode[3:]
            to_write = str(address) + ", " + str(zipcode.lstrip()) + ", " + str(clean.latitude) + ", " + str(clean.longitude)
            to_find = str(address)

            #print to_write

            # returns 'can not be cleaned' if street address has no numbers
            if any(i.isdigit() for i in str(address)):
                with open('/home/MY NAME/Address_Database.txt', 'a+') as database:
                    if to_find not in database.read():
                        database.write(dirty + '|' + to_write + '\n')
                if 'ncy rd' in address:
                    cleaned.append('<font color="red"> Can not be cleaned </font> <br>')
                    fail += 1
                elif 'nye rd' in address:
                    cleaned.append('<font color="red"> Can not be cleaned </font> <br>')
                    fail += 1
                elif 'nye c' in address:
                    cleaned.append('<font color="red"> Can not be cleaned </font> <br>')
                    fail += 1                    
                else:
                    cleaned.append(to_write + '<br>')
                    success += 1
            else:
                cleaned.append('<font color="red"> Can not be cleaned </font> <br>')
                fail += 1
        except AttributeError:
            cleaned.append('<font color="red"> Can not be cleaned </font> <br>')
            fail += 1
        except ValueError:
            cleaned.append('<font color="red"> Can not be cleaned </font> <br>')
            fail += 1
        except GeocoderTimedOut as e:
            cleaned.append('<font color="red"> Can not be cleaned </font> <br>')
            fail += 1

    total = success + fail
    percent = float(success) / float(total) * 100
    percent = round(percent, 2)
    print percent
    cleaned.append('<br>Accuracy: ' + str(percent) + ' %')
    cleaned.append('</p></center></body>')

    return "\n".join(cleaned)

更新:我已经切换为使用gunicorn运行应用程序,这解决了当我从家庭网络访问应用程序时遇到的问题,但是,在我的工作代理服务器中仍然收到TCP错误。我在控制台中没有收到任何错误消息,浏览器只显示TCP错误。我可以确定该工具仍在后台工作,因为我在循环中有一个打印语句告诉我每个地址仍在进行地理编码。这可能是由于我的工作网络不喜欢页面长时间保持加载状态,并最终显示代理错误页面所导致的吗?


你能放一些代码让我们看看吗? - Seekheart
@Seekheart 当然没问题。我会添加我的清理函数代码。 - Harrison
@slysid 那我应该使用nginx来托管这个应用程序吗? - Harrison
请查看 http://flask.pocoo.org/docs/0.11/deploying/ 以了解关于部署 Flask 应用程序的详细信息。另外,建议您使用 gevents,因为我认为您的服务器无法处理过多的请求。 - slysid
在Web应用程序的stderr中,是否存在“太多文件打开”错误?听起来像是文件句柄不足(默认为1024限制),您可以通过运行grep 'open' /proc/<webapp pid>检查限制,以及运行ls -1 /proc/<pid>/fd | wc -l以获取当前打开的文件句柄数。 - danny
显示剩余5条评论
2个回答

3
听起来好像是文件句柄不足(对于普通用户,默认限制为 1024),您可以通过运行 grep 'open' /proc/ 查看限制,以及 ls -1 /proc//fd | wc -l 查看当前打开的文件句柄数。

我认为你的代码没有正确发送响应,导致连接保持打开状态,最终耗尽了打开的文件句柄(在 posix 系统中,打开的套接字是一个文件)。

当出现问题时,可以使用 netstat -an | grep 确认连接的状态。它应该有一个包含 1k+ IP 和端口及其状态的列表。

我猜想它们处于 TIME_WAIT 状态,这表示客户端未能正确关闭连接,而留给内核稍后回收它们。

试一下:

from flask import make_response

@app.route("/clean", methods=['POST'])
def dothing():
    addresses = request.form['addresses']
    resp = make_response(cleanAddress(addresses), 200)
    return resp

如果是这种情况,我会预期套接字创建失败,而不是在已经打开的套接字上发送数据。更有可能的是,另一端已经断开了连接。 - Peter Brittain
这并没有解决问题。我认为问题是由于我的工作代理自动超时加载时间过长的页面所致。不过,我会授予您帮助的赏金... - Harrison

1

我遇到了类似的问题,使用合适的Web服务器解决了这个问题。我使用了UWSGI和nginx。


1
感谢您带我走过解决方案! - Harrison
这在我的家庭网络上可以工作,但是在我的工作网络上仍然出现相同的TCP错误。 - Harrison

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接