5

I want to use many thread with a timeout by thread. I already use the following code :

import concurrent.futures

class Action:

    def __init(self):
        self.name = 'initial'

    def define_name(self):
        # In my real code this method is executed during 30-60 seconds
        self.name = 'name'

action_list = [
    Action(),
    Action(),
]

with concurrent.futures.ThreadPoolExecutor(
    max_workers = 20
) as executor:
    for action_item in action_list:
        # without timeout arg, it works !
        executor.submit(action_item.define_name, timeout=1)

for action_item in action_list:
    print(action_item.name)

I already seen this post How to use concurrent.futures with timeouts? but I don't need to use result method

Python documentation don't help me (https://docs.python.org/3/library/concurrent.futures.html). It shows how to define timeout with map method but not with submit.

Do you know a way to define timeout with executor.submit method ?

Edit : This example is very simple. In my real case, I have a list of 15000+ items. Each action run by executor.submit() during 30 - 60 secondes. But some items action during more of 5 minutes, I want execpt Tiemout with these items.

Edit 2 : I want to stop a Thread after a timeout. But I don't want use Thread object. So this post (Is there any way to kill a Thread in Python?) don't resolve my problem. I want to use only concurrent.futures module.

Community
  • 1
  • 1
Samuel Dauzon
  • 10,744
  • 13
  • 61
  • 94
  • What is the `submit()` timeout supposed to do? `ThreadPoolExecutor` puts submitted items into a queue, this means `submit()` is not really a blocking operation. Blocking can only happen when the queue is full and the default queue has no limit. – dhke Sep 21 '15 at 08:31
  • Yes this example is very easy. I had completed the post with more details. – Samuel Dauzon Sep 21 '15 at 08:36
  • This does not add up with your code. Even with several thousand items, `submit()` should be practically instantaneous. `submit()` does only schedule a task for (later) execution it does not wait for the tasks to finish. There is nothing in this code that should make `submit()` take 30+ seconds. – dhke Sep 21 '15 at 08:46
  • Yes, I agree with you. Can you imagine that Action.define_name() method takes 30+ seconds ? I can't post the real code of my company. I posted a simple use case. – Samuel Dauzon Sep 21 '15 at 08:49
  • 1
    In that case you want to *interrupt* the thread *when* it is already running, not await its submission. `submit()` returns a `Future` object that has a `wait()` method with a timeout. You might be able to use that. But it might be more elegant to write the worker code so that it stops by itself when the timeout is reached. Simply killing a thread is usually not a good idea. – dhke Sep 21 '15 at 09:04
  • possible duplicate of [Is there any way to kill a Thread in Python?](http://stackoverflow.com/questions/323972/is-there-any-way-to-kill-a-thread-in-python) – dhke Sep 21 '15 at 09:13

1 Answers1

1

If you really want to async execution with timeout, you can pack your future inside another future so the last one can wait for main future and whole thing will be non-blocking. Like this:

executor.submit(lambda: executor.submit(func, arg).result(timeout=50))

But this is quite sloppy and ineffective.

Albert Bikeev
  • 375
  • 6
  • 16