我正在运行一个python脚本,它调用一个依赖于慢api的函数,而慢api又调用另一个同样依赖于同一慢api的函数。我想加快速度。你知道吗
最好的办法是什么?线程模块?如果是,请举例说明。关于线程,我注意到的一点是,你似乎无法从线程中检索返回值。。。我的大部分脚本都是用来打印函数的返回值的。。你知道吗
下面是我试图提高I/O性能的代码
def get_price_eq(currency, rate):
if isAlt(currency) == False:
currency = currency.upper()
price_eq = 'btc_in_usd*USD_in_'+str(currency)+'*'+str(rate)
#print price_eq
return price_eq
else:
currency = currency.lower()
price_eq = 'poloniex'+ str(currency) + '_close' + '*' + str(rate)
print(price_eq)
return price_eq
def get_btcprice_fiat(price_eq):
query = '/api/equation/'+price_eq
try:
conn = api.hmac(hmac_key, hmac_secret)
btcpricefiat = conn.call('GET', query).json()
except requests.exceptions.RequestException as e: # This is the correct syntax
print(e)
return float(btcpricefiat['data'])
usdbal = float(bal) * get_btcprice_fiat(get_price_eq('USD', 1))
egpbal = float(bal) * get_btcprice_fiat(get_price_eq('EGP', 1))
rsdbal = float(bal) * get_btcprice_fiat(get_price_eq('RSD', 1))
eurbal = float(bal) * get_btcprice_fiat(get_price_eq('EUR', 1))
如您所见,我调用get\u btc\u price,它从一个数据供应商那里调用一个缓慢的api,并传入另一个函数的结果,这个函数使用另一个api调用,我做了4次以上,我在寻找提高这个函数性能的方法?另外,我读到的一件事是,你不能从线程返回值?我的大多数代码返回值,然后打印给用户,我如何处理这个?你知道吗
python3具有Launching parallel tasks功能。这使我们的工作更容易。你知道吗
它对thread pooling和Process pooling有。你知道吗
下面给出了一个见解:
线程池执行器示例
处理池执行器
对于Python2.7,它将如下所示:
输出:
因此,在您的例子中,Python 3将如下所示:
因此,在您的情况下,Python2.7将如下所示:
相关问题 更多 >
编程相关推荐