Search code examples
pythonmultithreadingpython-asyncio

who can request to a RestAPI permanently with a specific time frame, without wait for request delay


this is my code for send request and receive data from rest api. but there is one problem: Sometimes the site responds late. how can i send my request without considering the site's response.

import time

import requests

file = open('save_data.csv', 'w')
url = 'http://127.0.0.1:8000/data'


def retrieve_data():
   try:
       response = requests.get(url)
       file.write(response.text + '\n')
       return True
   except:
       return False


interval = 0.5
while True:
   # add session to retrieve function
   result = retrieve_data()
   # if every thing ok sleep for 0.5 seconds
   if result:
       time.sleep(interval)
   else:
       # if we have any error , sleep for 5 second and continue run
       time.sleep(5)

after search find we have two choice:

  1. use Thread
  2. use asyncio

what is the best solution and how to do it?


Solution

  • I would go with a thread its simple to use.

    import threading
    
    thread = threading.Thread(target=func,args=arguments) #in your case you dont need the arguments
    
    thread.start() #start the thread
    

    i would also consider using a mutex or global bool to ensure that you dont try to write to the file even tho ist used by another thread

    You could use something like this :

    import time
    import threading
    import requests
    
    file = open('save_data.csv', 'w')
    url = 'http://127.0.0.1:8000/data'
    file_is_used = False
    
    def retrieve_data():
        global file_is_used
        try:
            response = requests.get(url)
            while file_is_used:
                time.sleep(0.1)
            file_is_used = True
            file.write(response.text + '\n')
            file_is_used = False
            return True
        except:
            file_is_used = False
            return False
    
    
    interval = 0.5
    while True:
        time.sleep(interval)
        if threading.active_count() >= 10: #allows a maximum of 10 concurrent Threads
            continue
        thread = threading.Thread(target=retrieve_data)
        thread.start()