Closing "Python Requests" connection from another thread -


to close application possible, can interrupt requests.post call thread , have terminate connection ?

i played adapters, no luck far:

for ad in self.client.session.adapters.values():     ad.close() 

the right way use message passing other thread. can poor-mans version of using shared global variable. example, can try running script:

#!/usr/bin/env python # test script verify can abort streaming downloads of large # files. import threading import time import requests  stop_download = false  def download(url):     r = requests.get(url, stream=true)     data = ''     content_gen = r.iter_content()      while (stop_download == false):         try:             data = r.iter_content(1024)         except stopiteration:             break      if (stop_download == true):         print 'killed other thread!'         r.close()  if __name__ == '__main__':     t = threading.thread(target=download,                           args=('http://ftp.freebsd.org/pub/freebsd/iso-images-amd64/9.1/freebsd-9.1-release-amd64-dvd1.iso',)                         ).start()     time.sleep(5)     stop_download = true     time.sleep(5) # make sure believe message stopped other thread. 

when doing in production, if don't have protection of gil, want use more caution around message-passing state avoid awkward multithreading bugs. i'm leaving implementor.


Comments