繁体   English   中英

python3的超时子线程

[英]Timeout child thread for python3

我对编程很陌生,我正在运行 Linux,python3.5

Stack Overflow 中也有一些类似的问题,但大部分都没有任何回应

like: [Python 2.7 多线程]在Python中,如何使子线程中的函数调用超时? , 和Python , 子线程上的函数超时而不使用信号和 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

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

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

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

https://github.com/dozysun/timeout-timer ,在使用子线程作为定时器的子线程中工作正常

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM