如何使用rxpython超时长时间运行的程序?

时间:2017-07-20 22:30:40

标签: python system.reactive reactive-programming rx-py

假设我有一个长期运行的python函数,看起来像这样?

import random
import time
from rx import Observable
def intns(x):
    y = random.randint(5,10)
    print(y)
    print('begin')
    time.sleep(y)
    print('end')
    return x

我希望能够设置超时1000ms

所以我喜欢这样的东西,创造一个可观察的并通过上面的强烈计算来映射它。

a = Observable.repeat(1).map(lambda x: intns(x))

现在,对于每个发出的值,如果需要超过1000毫秒,我想结束观察,只要我使用1000mson_error

到达on_completed
a.timeout(1000).subscribe(lambda x: print(x), lambda x: print(x))

上面的语句确实会超时,并调用on_error,但它继续完成计算强烈的计算,然后才返回到下一个语句。有没有更好的方法呢?

最后一个语句打印以下内容

8 # no of seconds to sleep
begin # begins sleeping, trying to emit the first value
Timeout # operation times out, and calls on_error
end # thread waits till the function ends

这个想法是,如果一个特定的函数超时,我希望能够继续我的程序,并忽略结果。

我想知道intns函数是否在一个单独的线程上完成,我想主线程在超时后继续执行,但我仍想停止在线程上计算intns函数,或者杀死不知何故。

5 个答案:

答案 0 :(得分:1)

以下是可以使用with timeout() :

调用的类

如果代码下的块运行时间超过指定时间,则会引发TimeoutError

import signal

class timeout:
    # Default value is 1 second (1000ms)
    def __init__(self, seconds=1, error_message='Timeout'):
        self.seconds = seconds
        self.error_message = error_message
    def handle_timeout(self, signum, frame):
        raise TimeoutError(self.error_message)
    def __enter__(self):
        signal.signal(signal.SIGALRM, self.handle_timeout)
        signal.alarm(self.seconds)
    def __exit__(self, type, value, traceback):
        signal.alarm(0)

# example usage
with timeout() :
    # infinite while loop so timeout is reached
    while True :
        pass

如果我理解你的功能,那么这就是你的实现:

def intns(x):
    y = random.randint(5,10)
    print(y)
    print('begin')
    with timeout() :
        time.sleep(y)
    print('end')
    return x

答案 1 :(得分:0)

您可以使用线程部分执行此操作 虽然没有特定的方法来杀死python中的线程,但是你可以实现一个方法来标记线程结束。

如果线程正在等待其他资源(在您的情况下,您通过随机等待模拟"长"正在运行的代码),这将无法工作

另见 Is there any way to kill a Thread in Python?

答案 2 :(得分:0)

这种方式有效:

=====Page 1. Chapter: Quick start=====
<h1>Quick start</h1>
Some text about quick start


=====Page 2. Chapter: Quick start=====
Some text about quick start



=====Page 3. Chapter: Installation=====

<h1>Installation</h1>
Some text about installation

=====Page 2. Chapter: Installation=====

Some text about installation

====

答案 3 :(得分:0)

这是一个超时的例子

import random
import time
import threading

_timeout = 0

def intns(loops=1):
    print('begin')
    processing = 0
    for i in range(loops):
        y = random.randint(5,10)
        time.sleep(y)
        if _timeout == 1:
            print('timedout end')
            return
        print('keep processing')
    return

# this will timeout
timeout_seconds = 10
loops = 10

# this will complete
#timeout_seconds = 30.0
#loops = 1

thr = threading.Thread(target=intns, args=([loops]), kwargs={})
thr.start()
st = time.clock();
while(thr.is_alive() == True):
    if(time.clock() - st > timeout_seconds):
        _timeout = 1

thr.join()
if _timeout == 0:
    print ("completed")
else:
    print ("timed-out")

答案 4 :(得分:0)

您可以使用time.sleep()并为time.clock()

创建一个while循环