I have a method which retrieves kwargs and future. I would like to save a dictionary of kwargs to future for later processing future result with kwargs.
class ThreadPoolExecutorImproved(object):
def __init__(self, max_workers):
self._executor = ThreadPoolExecutor(max_workers)
self._futures = {}
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
kwargs_to_exception = {}
for kwargs, future in self._futures.iteritems():
if future.exception():
kwargs_to_exception[kwargs] = future.exception
if kwargs_to_exception:
raise ThreadPoolException(kwargs_to_exception)
def submit(self, fn, *args, **kwargs):
future = self._executor.submit(fn, *args, **kwargs)
key = tuple(kwargs.items())
self._futures[key] = future
return future
However, I get an error on the line self.futures[key] = future
:
TypeError: unhashable type: 'dict' for python
why is it? I created a tuple from kwargs!
kwargs
.list
s or a genericobject
). So the problem is rather hard to solve in general, since it is not said these have hashable counterparts.kwargs
as the key in the first place? What if two functions are called with the samekwargs
, or what ifkwargs
is empty?