flaskgrequests

Additional arguments in Flask grequests hook


I am having issue in passing additional parameter to grequests using a hook, Its working in a standalone app (non flask) but its not with flask (flask integrated server) Here is my code snippet.

self.async_list = []
for url in self.urls:
   self.action_item = grequests.get(url, hooks = {'response' : [self.hook_factory(test='new_folder')]}, proxies={ 'http': 'proxy url'},timeout=20)
   self.async_list.append(self.action_item)

grequests.map(self.async_list)

def hook_factory(self, test, *factory_args, **factory_kwargs):
        print (test + "In start of hook factory") #this worked and I see test value is printing as new_folder
        def do_something(response, *args, **kwargs):
            print (test + "In do something") #This is not working hence I was not able to save this response to a newly created folder. 

            self.file_name = "str(test)+"/"
            print ("file name is " + self.file_name)
            with open(REL_PATH + self.file_name, 'wb') as f:    
                    f.write(response.content)
            return None
        return do_something

Am I missing anything here?.


Solution

  • Trying to answer my own question, After further analysis there was nothing wrong with the above code, for some reason I was not getting my session data which is in the request_ctx_stack.top. But the same session data was available in my h_request_ctx_stack._local, Don't know the reason. But I was able to get my data from h_request_ctx_stack._local instead _request_ctx_stack.top for this hook alone. After I made that change was able execute the same hook without any issues.