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

      • <bdo id='4SLny'></bdo><ul id='4SLny'></ul>
    1. <legend id='4SLny'><style id='4SLny'><dir id='4SLny'><q id='4SLny'></q></dir></style></legend>

      <small id='4SLny'></small><noframes id='4SLny'>

      来自信号处理程序的 sys.exit() 上的 Python 核心转储

      时间:2023-08-06

      <legend id='wCFNt'><style id='wCFNt'><dir id='wCFNt'><q id='wCFNt'></q></dir></style></legend>
        <tbody id='wCFNt'></tbody>
      <i id='wCFNt'><tr id='wCFNt'><dt id='wCFNt'><q id='wCFNt'><span id='wCFNt'><b id='wCFNt'><form id='wCFNt'><ins id='wCFNt'></ins><ul id='wCFNt'></ul><sub id='wCFNt'></sub></form><legend id='wCFNt'></legend><bdo id='wCFNt'><pre id='wCFNt'><center id='wCFNt'></center></pre></bdo></b><th id='wCFNt'></th></span></q></dt></tr></i><div id='wCFNt'><tfoot id='wCFNt'></tfoot><dl id='wCFNt'><fieldset id='wCFNt'></fieldset></dl></div>
        • <bdo id='wCFNt'></bdo><ul id='wCFNt'></ul>
        • <small id='wCFNt'></small><noframes id='wCFNt'>

          <tfoot id='wCFNt'></tfoot>
              • 本文介绍了来自信号处理程序的 sys.exit() 上的 Python 核心转储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我看到一个看似无害的程序的 python 核心转储.我编写了以下代码来演示我的问题:

                I am seeing python core dump for a seemingly harmless program. I have written following piece of code to demonstrate my problem:

                proc = None
                
                def __signalHandler(signum, frame):
                   print "In __signalHandler"
                
                   if proc is not None:
                      print "Send signal to BG proc"
                      os.killpg(os.getpgid(proc.pid), signal.SIGINT)
                
                      print "Wait for it to finish"
                      proc.communicate()
                
                   print "sys exit"
                   sys.exit(130)
                
                signal.signal(signal.SIGINT, __signalHandler)
                
                # Start the process
                proc = subprocess.Popen(["a.out"],
                                        stdout=subprocess.PIPE,
                                        stderr=subprocess.PIPE,
                                        preexec_fn=os.setsid)
                
                while proc.poll() is None:
                   try:
                      print proc.stdout.readline()
                   except:
                      print "Exception caught"
                
                print "Done!"
                

                a.out 是打印语句并循环休眠的可执行文件:

                a.out is a executable that prints statement and sleeps in a loop:

                int main(int argc, char* argv[])
                {
                   for (int i = 0; i < 100; i++)
                   {
                      printf("Sleeping...
                ");
                      std::cout.flush();
                      usleep(500000);
                   }
                
                   return 0;
                }
                

                以下是我运行 python 代码时得到的输出(我运行 python 程序,然后按 Ctrl-C 以便调用信号处理程序):

                Following is the output I get when I run the python code (I run the python program and then press Ctrl-C so that the signal handler gets invoked):

                $ python sample.py
                Sleeping...
                Sleeping...
                Sleeping...
                ^CIn __signalHandler
                Send signal to BG proc
                Wait for it to finish
                sys exit
                Segmentation fault (core dumped)
                

                有人知道为什么 python 核心转储吗?有没有什么办法解决这一问题?我的猜测是 sys.exit() 抛出的异常在某种程度上导致了问题.os._exit() 没有同样的问题.但是,我正在尝试找出这种行为背后的确切原因.

                Does anyone have a clue why python core dumps? Is there any way to fix this? My guess is that the exception thrown from sys.exit() is somehow causing a problem. os._exit() doesnt have the same issue. However, I am trying to find out the exact reason behind this behaviour.

                不管怎样,我使用的是 Python 2.7.3

                For what it's worth, I am using Python 2.7.3

                推荐答案

                这个版本有效:中断顶层程序正确打印sys exit",子进程的输出逐行打印.

                This version works: interrupting the top-level program correctly prints "sys exit", and output of the subprocess is printed line by line.

                import os, signal, subprocess, sys
                
                proc = None
                
                def __signalHandler(signum, frame):
                   print "In __signalHandler"
                
                   if proc is not None:
                      print "Send signal to BG proc"
                      os.killpg(os.getpgid(proc.pid), signal.SIGINT)
                
                      print "Wait for it to finish"
                      proc.communicate()
                
                   print "sys exit"
                   sys.exit(130)
                
                signal.signal(signal.SIGINT, __signalHandler)
                
                # Start the process
                proc = subprocess.Popen('ping -c3 8.8.8.8'.split(),
                                        stdout=subprocess.PIPE,
                                        stderr=subprocess.PIPE,
                                        preexec_fn=os.setsid)
                
                while proc.poll() is None:
                   try:
                      print proc.stdout.readline(),
                   except Exception as exc:
                      print 'Exception caught: {}'.format(exc)
                
                print "Done!"
                

                输出

                $ python ./sub5.py 
                
                PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
                64 bytes from 8.8.8.8: icmp_seq=1 ttl=43 time=48.0 ms
                64 bytes from 8.8.8.8: icmp_seq=2 ttl=43 time=48.3 ms
                ^CIn __signalHandler
                Send signal to BG proc
                Wait for it to finish
                sys exit
                
                $ echo $?
                130
                

                这篇关于来自信号处理程序的 sys.exit() 上的 Python 核心转储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:graphviz 分段错误 下一篇:在 Raspberry 上的 python 中,opencv 的分段错误

                相关文章

                最新文章

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

                <tfoot id='eAe6j'></tfoot>
                1. <legend id='eAe6j'><style id='eAe6j'><dir id='eAe6j'><q id='eAe6j'></q></dir></style></legend>
                    • <bdo id='eAe6j'></bdo><ul id='eAe6j'></ul>

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