I am trying to download large file from python requests library by setting the stream=True
But i want this function to be executed asynchronously and send response back to server with downloading in the background.
Here is my code
async def downloadFile(url, filename):
  r = requests.get(url, stream=True)
  with open(os.path.join('./files', filename), 'wb+') as f:
    for chunk in r.iter_content(chunk_size=1024):
        if chunk:
            f.write(chunk)
  # Creating same file name
  # with _done appended to know that file has been downloaded
  with open(os.path.join('./files', filename + '_done'), 'w+') as f: 
    f.close()
  await asyncio.sleep(1)
Calling this function from other function like this
# check if file exist in server
        if(os.path.exists(os.path.join('./files', fileName))):
            #file exist!!!
            #check if done file exist
            if(os.path.exists(os.path.join('./files', fileName + '_done'))):
                #done file exist
                self.redirect(self.request.protocol + "://" +
                              self.request.host + '/files/' + fileName)
            else:
                #done file not exist. Wait for 5 min more
                self.write('Wait 5 min')
                self.finish()
        else:
            # file doesnt exist. Initiate download
            self.write('Wait 5 min')
            self.finish()
            d = asyncio.ensure_future(downloadFile(
                fileRes, fileName))
            # loop = asyncio.get_event_loop()
            # loop.run_until_complete(d)
The problem is that the file is created but its size remains 0 and the file appended "_done" is never created. What am I doing wrong here?
 
    