0
我试图从各种API请求数据。服务器必须同时调用,我知道我需要使用多线程,但我无法弄清楚如何以我想要的方式返回数据,这里是一个示例。如何多线程函数,从API调用返回数据
import requests
import time
import threading
t = time.strftime('%m/%d/%Y %H:%M:%S')
def getBitstamp():
data = requests.get('https://www.bitstamp.net/api/ticker/')
data = data.json()
ask = round(float(data['ask']),2)
bid = round(float(data['bid']),2)
print 'bitstamp', time.strftime('%m/%d/%Y %H:%M:%S')
return ask, bid
def getBitfinex():
data = requests.get('https://api.bitfinex.com/v1/pubticker/btcusd')
data = data.json()
ask = round(float(data['ask']),2)
bid = round(float(data['bid']),2)
print 'finex', time.strftime('%m/%d/%Y %H:%M:%S')
return ask, bid
while True:
bitstampBid, bitstampAsk rate = thread.start_new_thread(getBitstamp)
bitfinexAsk, bitfinexBid = thread.start_new_thread(getBitfinex)
#code to save data to a csv
time.sleep(1)
似乎线程不喜欢返回多个(甚至任何值)我想我误解了库的工作原理。
编辑删除错误rate
变量
对不起'速度'并不意味着在那里。您链接到的帖子没有帮助 –
我解决了这个帖子http://stackoverflow.com/questions/41711357/python-how-to-multi-thread-a-function-that-returns-multiplevalue哈哈还注意到,恰巧是我在1年前将你的帖子与你联系在一起 –