Python Forum
How to timeout a task using the ThreadpoolExecutor?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to timeout a task using the ThreadpoolExecutor?
#1
Hi all.

I'm wonder what are the possible approaches to handle timeout of a task that was submitted to an executor ThreadPoolExecutor.
I know I can timeout while getting the result like so future.result(timeout=x). But I don't understand how can I timeout a task, in a non-blocking way, when I'm on "fire and forget" mode.

Example:

# Send the task to the pool
future = self.executor.submit(mytask, arg)
future.add_done_callback(...)

# By this time, I can `get` the result with timeout
future.result(timeout=xxx)
# Send the task to the pool
future = self.executor.submit(mytask, arg)
future.add_done_callback(...)

# By this time, I can `get` the result with timeout
future.result(timeout=xxx)
But if I call the result, I'll block, and I won't be able to send more tasks to the pool.
What I'm trying to do is something like:

# Notice the timeout would be defined when sending the task, and not when getting the result
future = self.executor.submit(mytask, arg, timeout=10)

# Assign the callback normally
future.add_done_callback(...)

# Here, I don't care about the result, I want to be able to submit another task
?
I though about opening a new "maintenance" thread, that only waits for the results of all tasks, but again, wouldn't I be blocking for each call?
Say I have 100 tasks periodically being sent to the executor, each just time.sleep(10). How can I make sure that none of those tasks pass 10 seconds?
If it helps, the context is a task queue, hence the "fire and forget" logic.

Any help/suggestion is welcome.
Thanks a lot!
Reply
#2
Hi,

I'm not sure if I understand your question correctly, but the concurent.futures module is not meant for a "fire and forgot" mode. The executors wait until tasks are finished. As the documentation for the ThreadPoolExecutor says: "All threads enqueued to ThreadPoolExecutor will be joined before the interpreter can exit.". Which means your problem blocks at one point.

If you are looking for background processing where new task can be fed any time, you may build something yourself using two threads or processes connected by a queue or you use an asyncronous task queue like e.g. Celery (or one of the lighter options).

Gruß, noisefloor
Reply
#3
I think you may be confused about what result timeout does. This is a limit on how long the caller waits for the processes to return a result. It does not set a maximum lifetime for the process.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  count certain task in task manager[solved] kucingkembar 2 2,557 Aug-29-2022, 05:57 PM
Last Post: kucingkembar
  TimeOut a function in a class ? Armandito 1 3,189 Apr-25-2022, 04:51 PM
Last Post: Gribouillis
  FTp timeout except korenron 2 6,430 Feb-01-2022, 06:51 AM
Last Post: korenron
  printing contents of Jar on timeout Rakshan 1 2,768 Jul-30-2021, 07:48 AM
Last Post: buran
  Schedule a task and render/ use the result of the task in any given time klllmmm 2 3,358 May-04-2021, 10:17 AM
Last Post: klllmmm
  How to create a task/import a task(task scheduler) using python Tyrel 7 6,817 Feb-11-2021, 11:45 AM
Last Post: Tyrel
  Trying to understand concurrent.futures.ThreadPoolExecutor Laxminarsaiah 0 2,530 Dec-18-2019, 12:43 PM
Last Post: Laxminarsaiah
  ThreadPoolExecutor read file to list DaLiPy 3 8,930 Jun-11-2019, 05:55 AM
Last Post: DaLiPy
  socket.timeout: timed out DanielGraham 2 26,958 Dec-22-2017, 06:07 PM
Last Post: DanielGraham
  FBProphet() Timeout in Anaconda sobrio1 0 4,331 Dec-21-2017, 05:15 AM
Last Post: sobrio1

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020