Operation I/O 'Block' loop?

Asked

Viewed 35 times

1

Greetings friends, I come here to ask for help in relation to doubt about the operations of Request and with the operation Open. As far as I know and read, the operation file_request = requests.get(url_file,timeout=3,stream=True) "Hangs" the execution of the main code (Thread) until the request is completed, doesn’t it? But at the time of writing the file, being it very large, if there is this "block", the execution of the loop also and stopped, or it creates a new thread for each I/O operation inside the loop?

My doubt and because of the time between requests, so that it is not barred by the server I ended up putting a time.sleep(3). Another question, that way of try/catch this correct, or would be better a nested? I tried to express as much as I could rsrs, please if I was not clear to me a touch so that I correct/ express myself better. Thank you to All!

for post in thread.find_all("div", { "class" : "file" }):
  filetext_a = post.select_one('div.fileText a')
  saveFile(filetext_a)
  time.sleep(3)
def saveFile(file_item):
  true_file_folder = download_folder + 'trueFiles/'
  url_file = urllib.parse.urljoin('https://',file_item['href'])
  try:  
    file_request = requests.get(url_file,timeout=3,stream=True)
    with open(true_file_folder + '%s' % file_item['href'].split('/')[4], 'wb') as f:
      for chunk in file_request.iter_content(chunk_size=128): 
            if chunk: 
                f.write(chunk) 
  except requests.exceptions.HTTPError as errh:
    print ("Http Error:",errh)
  except requests.exceptions.ConnectionError as errc:
    print ("Error Connecting:",errc)
  except requests.exceptions.Timeout as errt:
    print ("Timeout Error:",errt)
  except requests.exceptions.RequestException as err:
    print ("OOps: Something Else",err)
  except IOError as err:
    print ("I/O Error:",err)  
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.