Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
472 views
in Technique[技术] by (71.8m points)

How to correctly specify timeout in ThreadPoolExecutor (Python)?

I have the following code where I am trying to call functions with different timeouts. It might happen that the first function times out but the second one could have been executed in the specified time.

import time
from concurrent.futures import ThreadPoolExecutor

def test1(a, b, c):
    time.sleep(5)
    d=a+b+c
    print(d)

def test2(a, b):
    time.sleep(5)
    d=a+b
    print(d)

with ThreadPoolExecutor(max_workers=1) as executor1:
    try:
        executor1.submit(test1, 1,2,3).result(timeout=1)
    except:
        executor1.shutdown(wait=False)
        print("Pass")

with ThreadPoolExecutor(max_workers=1) as executor2:
    try:
        executor2.submit(test2, 1,2).result(timeout=8)
    except:
        executor2.shutdown(wait=False)
        print("Pass-2")

Expected Output

Pass
3

Actual Output

Pass
6
3

What I'd like to have is stop the execution of first executor as soon as there is a timeout. And continue with the next executor.

question from:https://stackoverflow.com/questions/65922433/how-to-correctly-specify-timeout-in-threadpoolexecutor-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Finally, it is implemented using this link. The final code is shared below :-

import time
from concurrent.futures import ThreadPoolExecutor
import ctypes

def terminate_thread(thread):
    """Terminates a python thread from another thread.

    :param thread: a threading.Thread instance
    """
    if not thread.isAlive():
        return

    exc = ctypes.py_object(SystemExit)
    res = ctypes.pythonapi.PyThreadState_SetAsyncExc(
        ctypes.c_long(thread.ident), exc)
    if res == 0:
        raise ValueError("nonexistent thread id")
    elif res > 1:
        # """if it returns a number greater than one, you're in trouble,
        # and you should call it again with exc=NULL to revert the effect"""
        ctypes.pythonapi.PyThreadState_SetAsyncExc(thread.ident, None)
        raise SystemError("PyThreadState_SetAsyncExc failed")

def test1(a, b, c):
    time.sleep(5)
    d=a+b+c
    print(d)

def test2(a, b):
    time.sleep(5)
    d=a+b
    print(d)

with ThreadPoolExecutor(max_workers=1) as executor:
    try:
        executor.submit(test1, 1,2,3).result(timeout=1)
    except:
        executor.shutdown(wait=False)
        for t in executor._threads:
            terminate_thread(t)
        print("Pass")

with ThreadPoolExecutor(max_workers=1) as executor:
    try:
        executor.submit(test2, 1,2).result(timeout=8)
    except:
        executor.shutdown(wait=False)
        for t in executor._threads:
            terminate_thread(t)
        print("Pass-2")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...