0

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

David Hancock
  • 453
  • 1
  • 11
  • 24

1 Answers1

0

You need to decide if you want to use high-level Threading module or low level thread. If later then use import thread and not import threading

Next, You also need to pass a tuple with the argument to run the function within thread. If you have none, you can pass an empty tuple as below.

bitstampBid, bitstampAsk, rate = thread.start_new_thread(getBitstamp,())
bitfinexAsk, bitfinexBid = thread.start_new_thread(getBitfinex,())

The program now executes, how-ever runs into separate errors which you may want to debug.
Here are couple of error I noticed.
getBitstamp() returns rate, however, rate is not defined within getBitstamp(). Program errors on this.

Python 2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>> 

Traceback (most recent call last):
  File "C:\amPython\multiThreading1.py", line 28, in <module>
    bitstampBid, bitstampAsk = thread.start_new_thread(getBitstamp,())
TypeError: 'int' object is not iterable
>>> bitstamp 03/18/2017 00:41:33

Some ideas on multi-threading are discussed on this SO post that may be useful for you.

Community
  • 1
  • 1
Anil_M
  • 10,893
  • 6
  • 47
  • 74
  • sorry `rate` wasn't meant to be in there. The post you linked to didn't help either – David Hancock Mar 18 '17 at 06:18
  • I solved it with this post http://stackoverflow.com/questions/41711357/python-how-to-multi-thread-a-function-that-returns-multiple-values Haha also notice that it was coincidently me who made that post you linked to 1 year ago – David Hancock Mar 18 '17 at 06:48