我正在尝试为一个位置自动完成 api 构建代理/缓存服务器。这是几乎在我尝试查询服务器时显示错误的简化代码:
#!/usr/bin/python
import gevent
from gevent import monkey
from gevent.wsgi import WSGIServer
monkey.patch_all()
import urllib2
import urlparse
import json
def requestHandler(env, start_response):
start_response('200 OK', [('Content-Type', 'text')])
parameters = urlparse.parse_qs(env['QUERY_STRING'])
if 'search' in parameters:
searchString = parameters['search'][0]
# Query the auto-completion server
json_results = urllib2.urlopen('http://autocomplete.wunderground.com/aq?query=' + searchString).read()
results = json.loads(unicode(json_results, "ISO-8859-1"))
finalResult = ''
for result in results['RESULTS']:
finalResult += result['name'] + ';' + result['c'] + ';' + result['zmw'] + ';' + result['tzs'] + ';'
return [finalResult.encode('utf-16')]
else:
return ['ERROR']
server = WSGIServer(('', 8888), requestHandler)
print 'Server running on port 8888...'
server.serve_forever()
有时它适用于第一个查询,但我第二次请求它时,它崩溃了。有时它第一次立即崩溃。这是我得到的错误:
Modules/gcmodule.c:348: visit_decref: Assertion "gc->gc.gc_refs != 0" failed.
refcount was too small
object : <weakref at 0x9e0f194; to 'gevent.core.http_request' at 0x9e0a11c>
type : weakref
refcount: 1
address : 0x9e0f194
Aborted (core dumped)
我的系统是:CentOS 6.3,Python 2.6.6,Gevent 0.13.8
有人知道可能出了什么问题吗?似乎很基本的东西会导致这种问题......