简体   繁体   English

python3的超时子线程

[英]Timeout child thread for python3

I am quite new to programming and I am running Linux, python3.5我对编程很陌生,我正在运行 Linux,python3.5

There are a few similar questions in Stack Overflow but most of them do not have any response Stack Overflow 中也有一些类似的问题,但大部分都没有任何回应

like: [Python 2.7 multi-thread]In Python, how to timeout a function call in sub-thread? like: [Python 2.7 多线程]在Python中,如何使子线程中的函数调用超时? , and Python , Timeout on a function on child thread without using signal and thread.join , 和Python , 子线程上的函数超时而不使用信号和 thread.join

I am able to use signal when it is in main thread and timeout for multiprocess.当信号在主线程和多进程超时时,我可以使用它。 However, the function I am currently running is a child thread using apscheduler (or it can be started directly)但是,我目前运行的函数是使用apscheduler的子线程(也可以直接启动)

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

and I can't convert it to child process because I am sharing database connection.而且我无法将其转换为子进程,因为我正在共享数据库连接。

I have also tried https://stackoverflow.com/a/36904264/2823816 , but terminal said我也试过https://stackoverflow.com/a/36904264/2823816 ,但终端说

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

in

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

I am now very sure how to solve it:( Thanks for any help.我现在非常确定如何解决它:(感谢您的帮助。

I gave up timing out the thread in my child thread.我放弃了子线程中的线程超时。

So I used multi-process within the child thread to kill it.所以我在子线程中使用了多进程来杀死它。 I could not find any other solution.我找不到任何其他解决方案。

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

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

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