Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #103144 > unrolled thread

Re: Passing data across callbacks in ThreadPoolExecutor

Started by"Joseph L. Casale" <jcasale@activenetwerx.com>
First post2016-02-18 19:06 +0000
Last post2016-02-18 19:06 +0000
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Passing data across callbacks in ThreadPoolExecutor "Joseph L. Casale" <jcasale@activenetwerx.com> - 2016-02-18 19:06 +0000

#103144 — Re: Passing data across callbacks in ThreadPoolExecutor

From"Joseph L. Casale" <jcasale@activenetwerx.com>
Date2016-02-18 19:06 +0000
SubjectRe: Passing data across callbacks in ThreadPoolExecutor
Message-ID<mailman.18.1455822696.2289.python-list@python.org>
On Thur, Feb 17, 2016 at 9:24 AM, Ian Kelly <ian.g.kelly@gmail.com> wrote:
>> What is the pattern for chaining execution of tasks with ThreadPoolExecutor?
>> Callbacks is not an adequate facility as each task I have will generate new output.
>
> Can you specify in more detail what your use case is?
>
> If you don't mind having threads sitting around waiting, you can just
> submit each chained task at the start and have each task wait on the
> futures of its dependencies. The advantage of this is that it's easy
> to conceptualize the dependency graph of the tasks. The disadvantage
> is that it eats up extra threads. You'll probably want to increase the
> size of the thread pool to handle the waiting tasks (or use a separate
> ThreadPoolExecutor for each chained task).

The thing with callbacks is each one gets the original tasks result. So the
callback can't pass a new task result up and/or cancel the task "set".

> Otherwise, is there some reason you can't use multiple callbacks, one
> to handle the task's output and one to submit the chained task?
> 
> E.g.:
> 
> def chain_task2(input, f2):
>     f2 = executor.submit(task2, input, f2.result())
>     f2.add_done_callback(handle_task2_done)
> 
> f1 = executor.submit(task1, input)
> f1.add_done_callback(handle_task1_done)
> f1.add_done_callback(functools.partial(chain_task2, input))

Closing over the executer instance seems potentially race'y. What happens when
the last task finishes and the executer checks (locking the queue) and the task also
wants to add more work. I'll check the source, but I had hoped for a more succinct way.

Thanks,
jlc

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web