简体   繁体   English

什么是实现线程最大执行时间的最佳方法(在python / gtk中)?

[英]what is best way to implement thread maximum execution time (in python/gtk)?

What is the best known practice to implement a thread job timeout (ie: kill the job after a maximum of X seconds) ? 什么是实现线程作业超时(即:最多X秒后终止作业)的最佳实践? I wrote the following python code. 我编写了以下python代码。 I read a lot of different way to implement this but I'm a little lost... Do I have to do this with a timer ? 我读到了很多不同的实现方法,但是我有点迷失了……我必须使用计时器吗? or by counting in add_timeout callback ? 或通过计数add_timeout回调?

as a side note , usage of thread.join(timeout) is pretty limited within gtk/threaded application as it blocks the main thread ? 作为附带说明,在gtk / threaded应用程序中, thread.join(timeout)用法非常有限,因为它阻塞了主线程?

Thanks !! 谢谢 !!

Note: I'm pretty new to python/threading 注意:我是python / threading的新手

#!/usr/bin/python

import time
import threading
import gobject
import gtk
import glib
gobject.threads_init()


class myui():
    def __init__(self):
        interface = gtk.Builder()
        interface.add_from_file("myui.glade")
        interface.connect_signals(self)

        self.spinner = interface.get_object('spinner1')         

    def bg_work1(self):
        print "work has started"
        # simulates some work
        time.sleep(5)
        print "work has finished"

    def startup(self):
        thread = threading.Thread(target=self.bg_work1)
        thread.start()

        # work started. Now while the work is being done I want
        # a spinner to rotate
        self.spinner.start()
        print "spinner started"

        #thread.join() # I wanna wait for the job to be finished while the spinner spins.
                                    # but this seems to block the main thread, and so the gui doesn't shows up !

        glib.timeout_add(100, self.check_job, thread, 5)

    def check_job(self, thread, timeout):
        #print 'check job called'

        if not thread.isAlive():
                print 'is not alive anymore'
                self.spinner.stop()
                return False
        return True


if __name__ == "__main__":

    app = myui()
    app.startup()

    print "gtk main loop starting !"

    gtk.main()
    print "gtk main loop has stopped !"

Your code seems pretty good, the only thing missing is to use and evaluate a timeout point: 您的代码看起来非常不错,唯一缺少的是使用和评估超时点:

    glib.timeout_add(100, self.check_job, thread, time.time() + 5)

def check_job(self, thread, timeout):
    #print 'check job called'

    if not thread.isAlive():
            print 'is not alive anymore'
            self.spinner.stop()
            return False
    if time.time() > timeout:
            print 'job timed out'
            self.spinner.stop()
            return False
    return True

However, this does not kill your worker thread after the timeout, so it will still be running. 但是,这不会在超时后终止您的工作线程,因此它仍将运行。 I am not aware of a method to forcibly terminate a Python thread. 我不知道强制终止Python线程的方法。 You would have to split your work and check for timeout in the worker thread, so it can gracefully terminate. 您将不得不分割工作并在工作线程中检查超时,以便可以正常终止。

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

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