python 實現超時退出的兩種方式

基於 signal模塊實現:

signal包負責在Python程序內部處理信號,典型的操作包括預設信號處理函數,暫 停並等待信號,以及定時發出SIGALRM等。要注意,signal包主要是針對UNIX平臺(比如Linux, MAC OS),而Windows內核中由於對信號機制的支持不充分,所以在Windows上的Python不能發揮信號系統的功能。 



# coding:utf8
import time
import signal


# 自定義超時異常
class TimeoutError(Exception):
    def __init__(self, msg):
        super(TimeoutError, self).__init__()
        self.msg = msg


def time_out(interval, callback):
    def decorator(func):
        def handler(signum, frame):
            raise TimeoutError("run func timeout")

        def wrapper(*args, **kwargs):
            try:
                signal.signal(signal.SIGALRM, handler)
                signal.alarm(interval)       # interval秒後向進程發送SIGALRM信號
                result = func(*args, **kwargs)
                signal.alarm(0)              # 函數在規定時間執行完後關閉alarm鬧鐘
                return result
            except TimeoutError, e:
                callback(e)
        return wrapper
    return decorator


def timeout_callback(e):
    print(e.msg)


@time_out(2, timeout_callback)
def task1():
    print("task1 start")
    time.sleep(3)
    print("task1 end")


@time_out(2, timeout_callback)
def task2():
    print("task2 start")
    time.sleep(1)
    print("task2 end")


if __name__ == "__main__":
    task1()
    task2()

輸出: 

 task1 start
 run func timeout
 task2 start
 task2 end

 基於子線程阻塞實現超時:

# coding:utf8
import time
import threading


def callback_func():
    print('超時回調')


def time_out(interval, callback=None):
    def decorator(func):
        def wrapper(*args, **kwargs):
            t =threading.Thread(target=func, args=args, kwargs=kwargs)
            t.setDaemon(True)  # 設置主線程技術子線程立刻結束
            t.start()
            t.join(interval)  # 主線程阻塞等待interval秒
            if t.is_alive() and callback:
                return threading.Timer(0, callback).start()  # 立即執行回調函數
            else:
                return
        return wrapper
    return decorator


@time_out(2, callback_func)
def task3(hh):
    print('**********task3****************')
    for i in range(3):
        time.sleep(1)
        print(i)
        print(hh)


@time_out(2, callback_func)
def task4(hh):
    print('**********task4****************')
    for i in range(3):
        # time.sleep(1)
        print(i)
        print(hh)


if __name__ == '__main__':
    task3('參數')
    task4('參數')

輸出:

**********task3****************
0
參數
1
參數
超時回調
**********task4****************
0
參數
1
參數
2
參數

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章