0

I have a function batch_opt taking two arguments (integer i and pandas dataframe train) and return a python dictionary. When I was trying to parallelize the computation using DASK in Python, I got the type error of Delayed objects are immutable. I am new to DASK. Can anyone help me out here? Thanks.

results = []
for i in range(0, 2):
    validation_res = delayed(batch_opt)(i, train)
    results.append(validation_res) 

start = time.time()
res = compute(*results)
print(time.time() - start)

Trace:

TypeError                                 Traceback (most recent call last)
    <ipython-input-19-8463f64dec56> in <module>
          5 
          6 start = time.time()
    ----> 7 res = compute(*results)
          8 print(time.time() - start)

~/.conda/envs/odop/lib/python3.8/site-packages/dask/base.py in compute(*args, **kwargs)
    568         postcomputes.append(x.__dask_postcompute__())
    569 
--> 570     results = schedule(dsk, keys, **kwargs)
    571     return repack([f(r, *a) for r, (f, a) in zip(results, postcomputes)])
    572 

~/.conda/envs/odop/lib/python3.8/site-packages/dask/threaded.py in get(dsk, result, cache, num_workers, pool, **kwargs)
     77             pool = MultiprocessingPoolExecutor(pool)
     78 
---> 79     results = get_async(
     80         pool.submit,
     81         pool._max_workers,

~/.conda/envs/odop/lib/python3.8/site-packages/dask/local.py in get_async(submit, num_workers, dsk, result, cache, get_id, rerun_exceptions_locally, pack_exception, raise_exception, callbacks, dumps, loads, chunksize, **kwargs)
    505                             _execute_task(task, data)  # Re-execute locally
    506                         else:
--> 507                             raise_exception(exc, tb)
    508                     res, worker_id = loads(res_info)
    509                     state["cache"][key] = res

~/.conda/envs/odop/lib/python3.8/site-packages/dask/local.py in reraise(exc, tb)
    313     if exc.__traceback__ is not tb:
    314         raise exc.with_traceback(tb)
--> 315     raise exc
    316 
    317 

~/.conda/envs/odop/lib/python3.8/site-packages/dask/local.py in execute_task(key, task_info, dumps, loads, get_id, pack_exception)
    218     try:
    219         task, data = loads(task_info)
--> 220         result = _execute_task(task, data)
    221         id = get_id()
    222         result = dumps((result, id))

~/.conda/envs/odop/lib/python3.8/site-packages/dask/core.py in _execute_task(arg, cache, dsk)
    117         # temporaries by their reference count and can execute certain
    118         # operations in-place.
--> 119         return func(*(_execute_task(a, cache) for a in args))
    120     elif not ishashable(arg):
    121         return arg

<ipython-input-7-e3af5748e1cf> in batch_opt(i, train)
     22         test.loc[:, 'seg'] = test.apply(lambda x: proc.assign_trxn(x), axis = 1)
     23         test_policy_res, test_metrics_res = opt.analyze_result(fa_m, x, test, cum_to_day, cur_policy, policy)
---> 24         validation_res[(train_mon_yr_batch, test_mon_yr)] = {'train_policy': train_policy_res, 'train_result': train_metrics_res, 'test_policy': test_policy_res, 'test_result': test_metrics_res}
     25     return validation_res

~/.conda/envs/odop/lib/python3.8/site-packages/dask/delayed.py in __setitem__(self, index, val)
    564 
    565     def __setitem__(self, index, val):
--> 566         raise TypeError("Delayed objects are immutable")
    567 
    568     def __iter__(self):

TypeError: Delayed objects are immutable
Undecided
  • 611
  • 8
  • 13
  • 1
    Can you post the full [traceback](https://realpython.com/python-traceback/) and a function that reproduces the problem? It's really hard for us to help without more information - ideally a [minimal reproducible example](/help/minimal-reproducible-example). Thanks! – Michael Delgado Oct 29 '21 at 01:08
  • I couldn't reproduce the error. – Bill Oct 29 '21 at 15:12
  • Is `validation_res` in your traceback (inside the delayed function call) the same as the variable `validation_res` to which you assign the delayed function results? because that looks like your issue. otherwise, we need a MRE or at least we need to know what's in the function if we're going to be able to help. This is an issue with the code you're not showing us, not with dask ;) – Michael Delgado Oct 29 '21 at 23:36

0 Answers0