Samuel Dauzon
Samuel Dauzon

Reputation: 11324

Python concurrent futures executor.submit timeout

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.

Upvotes: 5

Views: 6799

Answers (1)

Albert Bikeev
Albert Bikeev

Reputation: 387

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.

Upvotes: 2

Related Questions