python3的超时子线程

时间:2017-10-20 04:22:57

标签: python multithreading timeout

我是编程新手,我正在运行Linux,python3.5

Stack Overflow中有一些类似的问题,但大多数都没有任何响应

喜欢:[Python 2.7 multi-thread]In Python, how to timeout a function call in sub-thread?Python , Timeout on a function on child thread without using signal and thread.join

我可以在主线程中使用信号,在多进程时超时。但是,我当前运行的函数是使用apscheduler的子线程(或者它可以直接启动)

schedule.add_job(test_upload.run, 'interval', seconds=10, start_date='2016-01-01 00:00:05',
                    args=['instant'])

我无法将其转换为子进程,因为我正在共享数据库连接。

我也试过https://stackoverflow.com/a/36904264/2823816,但终端说

  result = await future.result(timeout = timeout)
                        ^
SyntaxError: invalid syntax

import concurrent

def run():
    return 1

timeout = 10

with concurrent.futures.ThreadPoolExecutor(max_workers=1) as executor:
    future = executor.submit(run) # get a future object
    try:
        result = await future.result(timeout = timeout)
    except concurrent.futures.TimeOutError:
        result = None

我现在非常确定如何解决它:(感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

我放弃了我的子线程中的线程超时。

所以我在子线程中使用了多进程来杀死它。我找不到任何其他解决方案。

相关问题