title: Python threading requests json post
body: <p>I try to do multithreading post requests to an api json, goes well some threads but shortly receive error from one of threads,</p>
  
  <blockquote>
    <p>ValueError: No JSON object could be decoded</p>
  </blockquote>
  
  <p>My code:</p>
  
  <pre><code>import threading
  import requests
  import json
  import random
  
  exitFlag = 0
  
  class myThread (threading.Thread):
      def __init__(self, threadID, name, key):
          threading.Thread.__init__(self)
          self.threadID = threadID
          self.name = name
          self.key = key
  
      def run(self):
          print "Starting " + self.name
          do_bet(self.name, self.key)
          print "Exiting " + self.name
  
  def do_bet(threadName, key):
      while True:
  
          #do post on api
  
          def change_seed():
              payload = {'seed': random.randrange(100, 1000, 2)}
              requests.post('https://url.com/api?api_key=' + str(key), data=payload)
  
          def high(amount):
              payload = {'amount': amount, 'target': '90.00', 'condition': '&lt;'}
              bet = requests.post('https://api.url.com/api?api_key=' + str(key), data=payload)
              win_bet = bet.json()['bet']['win']
              roll_bet = bet.json()['bet']['roll']
              return win_bet, roll_bet
          def low(amount):
              payload = {'amount': amount, 'target': '9.99', 'condition': '&gt;'}
              bet = requests.post('https://api.url.com/api?api_key=' + str(key), data=payload)
              win_bet = bet.json()['bet']['win']
              roll_bet = bet.json()['bet']['roll']
              return win_bet, roll_bet
  
          def onloss(horl):
              if horl == 0:
                  win, roll = low(100)
                  if win == False:
                      change_seed()
                      win, roll = high(1000)
                      if win == False:
                          low(0)
              elif horl == 1:
                  win, roll = high(100)
                  if win == False:
                      change_seed()
                      win, roll = low(1000)
                      if win == False:
                          high(0)
  
          win, roll = high(10)
          if win == False:
              onloss(0)
  
          win, roll = low(10)
          if win == False:
              onloss(1)
  
  
  # Create new threads
  thread1 = myThread(1, "First", "8asd8a-hd7837hda-837g78-a37rjf3")
  thread2 = myThread(2, "Two", "f56fah-sddddjf74f-kkkdj-fdbf446")
  
  # Start new Threads
  thread1.start()
  thread2.start()
  </code></pre>
  
  <p>Does anyone have any idea how to make them work properly both in parallel?</p>