<tfoot id='HN4wO'></tfoot><legend id='HN4wO'><style id='HN4wO'><dir id='HN4wO'><q id='HN4wO'></q></dir></style></legend>

    <small id='HN4wO'></small><noframes id='HN4wO'>

    1. <i id='HN4wO'><tr id='HN4wO'><dt id='HN4wO'><q id='HN4wO'><span id='HN4wO'><b id='HN4wO'><form id='HN4wO'><ins id='HN4wO'></ins><ul id='HN4wO'></ul><sub id='HN4wO'></sub></form><legend id='HN4wO'></legend><bdo id='HN4wO'><pre id='HN4wO'><center id='HN4wO'></center></pre></bdo></b><th id='HN4wO'></th></span></q></dt></tr></i><div id='HN4wO'><tfoot id='HN4wO'></tfoot><dl id='HN4wO'><fieldset id='HN4wO'></fieldset></dl></div>

      • <bdo id='HN4wO'></bdo><ul id='HN4wO'></ul>

      如何在不连续检查标志的情况下终止 Python 线程

      时间:2023-07-21
      <legend id='CCcMV'><style id='CCcMV'><dir id='CCcMV'><q id='CCcMV'></q></dir></style></legend>

        <tbody id='CCcMV'></tbody>

          <small id='CCcMV'></small><noframes id='CCcMV'>

            • <bdo id='CCcMV'></bdo><ul id='CCcMV'></ul>
              <i id='CCcMV'><tr id='CCcMV'><dt id='CCcMV'><q id='CCcMV'><span id='CCcMV'><b id='CCcMV'><form id='CCcMV'><ins id='CCcMV'></ins><ul id='CCcMV'></ul><sub id='CCcMV'></sub></form><legend id='CCcMV'></legend><bdo id='CCcMV'><pre id='CCcMV'><center id='CCcMV'></center></pre></bdo></b><th id='CCcMV'></th></span></q></dt></tr></i><div id='CCcMV'><tfoot id='CCcMV'></tfoot><dl id='CCcMV'><fieldset id='CCcMV'></fieldset></dl></div>
                <tfoot id='CCcMV'></tfoot>
                本文介绍了如何在不连续检查标志的情况下终止 Python 线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                class My_Thread(threading.Thread):
                
                    def __init__(self):
                        threading.Thread.__init__(self)
                
                    def run(self):
                        print "Starting " + self.name
                        cmd = [ "bash", 'process.sh']
                        p = subprocess.Popen(cmd,
                                     stdout=subprocess.PIPE,
                                     stderr=subprocess.STDOUT)
                        for line in iter(p.stdout.readline, b''):
                            print ("-- " + line.rstrip())
                        print "Exiting " + self.name
                
                    def stop(self):
                        print "Trying to stop thread "
                        self.run = False
                
                thr = My_Thread()
                thr.start()
                time.sleep(30)
                thr.stop()
                thr.join()
                

                所以我有上面显示的线程,实际上在 windows 上工作,process.sh 是在 cygwin 中运行的 bash 脚本,大约需要 5 分钟才能完成执行,所以它不是一个循环,它是一些模拟过程

                So i have thread like show above, actually work on windows and process.sh is bash script which run in cygwin and takes around 5 min to finish execution so its not a loop its some simulation proecess

                我想在这个类中创建 stop() 函数,以便我可以在需要时立即终止脚本.终止后,我不希望 process.sh 脚本有任何有用的结果

                i want to create stop() function in this class so that i can terminate script immediately when i want. after termination i am not expecting any useful result from process.sh script

                请你建议任何方法,如果可能的话也请给出一点解释..

                please can u suggest any method, If possible please give little explanation too..

                推荐答案

                对于您的特定示例,通过使用 Popen 对象的 terminate() 方法...

                For your particular example, it's probably easiest to terminate the thread by terminating the subprocess it spawns using the Popen object's terminate() method...

                class My_Thread(threading.Thread):
                
                    def __init__(self):
                        threading.Thread.__init__(self)
                        self.process = None
                
                    def run(self):
                        print "Starting " + self.name
                        cmd = [ "bash", 'process.sh']
                        self.process = p = subprocess.Popen(cmd,
                                     stdout=subprocess.PIPE,
                                     stderr=subprocess.STDOUT)
                        for line in iter(p.stdout.readline, b''):
                            print ("-- " + line.rstrip())
                        print "Exiting " + self.name
                
                    def stop(self):
                        print "Trying to stop thread "
                        if self.process is not None:
                            self.process.terminate()
                            self.process = None
                
                thr = My_Thread()
                thr.start()
                time.sleep(30)
                thr.stop()
                thr.join()
                

                ...导致 SIGTERM 被发送到 bash,并且下一次调用 p.stdout.readline() 以引发一个异常,将终止线程.

                ...causing a SIGTERM to be sent to bash, and the next call to p.stdout.readline() to raise an exception, which will terminate the thread.

                这篇关于如何在不连续检查标志的情况下终止 Python 线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:如何从 Python 迭代器提供子进程的标准输入? 下一篇:在 Python 中使用斯坦福正则表达式

                相关文章

                  • <bdo id='M3X6j'></bdo><ul id='M3X6j'></ul>

                  <small id='M3X6j'></small><noframes id='M3X6j'>

                1. <legend id='M3X6j'><style id='M3X6j'><dir id='M3X6j'><q id='M3X6j'></q></dir></style></legend>

                2. <tfoot id='M3X6j'></tfoot>
                    <i id='M3X6j'><tr id='M3X6j'><dt id='M3X6j'><q id='M3X6j'><span id='M3X6j'><b id='M3X6j'><form id='M3X6j'><ins id='M3X6j'></ins><ul id='M3X6j'></ul><sub id='M3X6j'></sub></form><legend id='M3X6j'></legend><bdo id='M3X6j'><pre id='M3X6j'><center id='M3X6j'></center></pre></bdo></b><th id='M3X6j'></th></span></q></dt></tr></i><div id='M3X6j'><tfoot id='M3X6j'></tfoot><dl id='M3X6j'><fieldset id='M3X6j'></fieldset></dl></div>