Python信号包无法超时吗?

时间:2019-01-09 16:33:06

标签: python signals

因此,我有一个Python脚本,该脚本将从3D对象的表面采样以获取对象的点云表示形式。

我大约有85,000个数据文件,其中一些实际上是2D对象,并将使执行转换的功能永久运行。我计划进行follow this approach,使用signal.alarm()函数和try except来跳过运行时间过长(超过5秒)的案例。

但是,以下代码似乎无效。该功能仍然停留在有问题的文件中...

如果您有时间,可以帮助您确定可能的原因吗?我使用signal.alarm()函数错误吗?谢谢!

我在Linux计算机Ubuntu 16.04上

有问题的代码发生在try except函数的main函数的P = tri2pts(V, G, 2048)块中。复制其余代码只是为了保持一致。

import numpy as np
import pyximport; pyximport.install(inplace=True, reload_support=True)
from _trimesh2pointcloud import cy_trimesh2pointcloud as tri2pts
import signal


def handler(signum, frame):
    print("Forever is over!")
    raise Exception("end of time")


def strip_slash(str1):
    slash_pos = str1.find('/')
    if slash_pos > -1:
        return int(str1[0:slash_pos])
    else:
        return int(str1)

def read_obj(fname):
    with open(fname) as f:
        content = f.readlines()

    content = [x.strip('\n') for x in content]
    content = [x.split() for x in content]

    type_col = np.array([row[0] for row in content])
    v_ind = np.where(type_col == 'v')[0]
    f_ind = np.where(type_col == 'f')[0]

    content = [x[1:4] for x in content]
    content = np.array(content)
    vertices = content[v_ind].tolist()
    faces = content[f_ind].tolist()

    vertices = np.array([[float(y) for y in x] for x in vertices])
    faces = np.array([[strip_slash(y) - 1 for y in x] for x in faces])
    return vertices, faces


def main():
    base_dir = '/media/yuqiong/DATA/problems'
    flist = os.listdir(base_dir)

    for f in flist:
        V, G = read_obj(os.path.join(base_dir, f))
        print("Now process file ")
        print(f)
        signal.signal(signal.SIGALRM, handler)
        signal.alarm(5)
        try:
            P = tri2pts(V, G, 2048)
        except Exception as exc:
            print(exc)
            continue

if __name__ == '__main__':
    main()

0 个答案:

没有答案