I'm trying to request data from various APIs. The servers must be called at the same time, I know I need to use multi threading but I can't figure out how to return the data in the way I want, here's an example.
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)
It seems however the thread doesn't like return multiple (or even any values) I think I have misunderstood how the library works.
EDIT removed error rate
variable