我试图做到这一点API调用代码:(名为)IPython中不执行像蟒蛇
# -*- coding: utf-8 -*-
from tornado.httpclient import AsyncHTTPClient
http_client = AsyncHTTPClient()
url = ''
response = ''
def meteo(q='', key=''):
if len(key) != 29:
print 'Please provide a valid key'
elif q == '':
print 'please provide a valid city name or zip'
global url
url = 'http://api.worldweatheronline.com/free/v2/weather.ashx?q={0}&format=json&key= {1}'.format(q, key)
def handle_request(resp):
global response
if resp.error:
print "Error:", resp.error
else:
response = resp.body
http_client.fetch(url, handle_request)
如果我尝试调用该文件,并从普通的Python控制台执行它,我不没有得到response
,其中''
。
但是,如果我使用IPython控制台调用它,或者使用Spyder执行它,它会调用response
,即使我没有初始化它,它也会被调用!
import wwo
wwo.meteo(q='London', key='API_FREE_KEY')
wwo.response # returns '' on normal python console, returns the full response on ipython console
啊,所以这就是为什么,这个想法是试图将阻塞'urlopen'转换为异步客户端,我可以在不使用IOLoop的情况下使用它[ – Abdelouahab 2014-12-04 21:34:27
]我做到了这一点:D https://github.com/ abdelouahabb/tornadwwo – Abdelouahab 2014-12-04 22:58:34
看来,现在我得到它更好,当我得到错误,当我尝试运行里面的Spyder龙卷风计划,并返回我这个'RuntimeError:IOLoop已经运行'再次谢谢你! – Abdelouahab 2014-12-04 23:24:50