'How do I use pythons concurrent.futures sessions in a class

I'm trying to use concurrent futures in a class like this:

from concurrent.futures import as_completed
from concurrent.futures import ProcessPoolExecutor
from requests_futures.sessions import FuturesSession
import requests

class z:
    def __init__(self):
        self.setJWT()

    def setJWT(self):
        response = requests.post(uri, headers=headers,data=json.dumps({'username':username, 'password': password})).json()
        try:
            self._jwt = response['jwt']
            self._headers = {'Authorization':f'Bearer {self._jwt}','Content-Type':'application/json'}
        except KeyError as e:
            print(response)

    def _p(func): #maybe this should have been a decorator
        def wrapper(self,*args,**kwargs):
            try:
                return func(self,*args,**kwargs)
            except:
                self.setJWT()
                return func(self,*args,**kwargs)
        return wrapper
    @_p
    def _b(self,s,session):
        data = json.dumps({'matchCompanyInput':[{'companyId': ids} for ids in s]})
        future = session.post(url, headers=str(self._headers), data=data, timeout = 30)
        return future

    def bulk(self,s):
        session = FuturesSession(executor=ProcessPoolExecutor(max_workers=10))
        futures = [self._b(s[i * 25:(i + 1) * 25],session = session) for i in range((len(s) + 25 - 1) // 25 )]
        resp = []
        for future in as_completed(futures):
            try:
                resp += future.result()
            except Exception as err:
                logging.info(err)
        return resp

za = z()
za. z.bulk([1,2,3])

But I get authentication errors. If I just use pdb, go to line "return future" and type in:

 future = session.post(url, headers=str(self._headers), data=data, timeout = 30)

and wait until its done, I get no 401 error and it works fine, so I think when I run it straight, the headers value disappears or some other asynchronous magic. Also, my wrapper only works if I raise an error, but since its asynchronous, I don't know where to raise the error.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source