简体   繁体   English

错误:分段错误(核心已转储)

[英]Error: Segmentation fault (core dumped)

Im new in python and am getting a strange error:我是 python 的新人,我收到一个奇怪的错误:

Segmentation fault (core dumped)

When i execute the following code:当我执行以下代码时:

  class Workspace(QMainWindow, Ui_MainWindow):
    """ This class is for managing the whole GUI `Workspace'.
        Currently a Workspace is similar to a MainWindow
    """

    def __init__(self):
        #p= subprocess.Popen(["java -Xmx256m -jar bin/HelloWorld.jar"],cwd=r'/home/karen/sphinx4-1.0beta5-src/sphinx4-1.0beta5/', shell=True, stdout=subprocess.PIPE, bufsize= 4024)
        try:
            from Queue import Queue, Empty
        except ImportError:
            while True:
    #from queue import Queue, Empty  # python 3.x
                print "error"

        ON_POSIX = 'posix' in sys.builtin_module_names

        def enqueue_output(out, queue):
            for line in iter(out.readline, b''):
                queue.put(line)
            out.close()

        p= Popen(["java -Xmx256m -jar bin/HelloWorld.jar"],cwd=r'/home/karen/sphinx4-1.0beta5-src/sphinx4-1.0beta5/',stdout=PIPE, shell=True, bufsize= 4024)
        q = Queue()
        t = threading.Thread(target=enqueue_output, args=(p.stdout, q))
        #t = Thread(target=enqueue_output, args=(p.stdout, q))
        t.daemon = True # thread dies with the program
        t.start()

# ... do other things here
        def myfunc(q):
            while True:

                try: line = q.get_nowait()
         # or q.get(timeout=.1)
                except Empty:
                    print('Vacio')
                else: # got line
    # ... do something with line

                    print line  


        thread = threading.Thread(target=myfunc, args=(q,))
        thread.start()

This part of code is reading from a program's stdout.When i execute myfunc out of the thread it works.这部分代码是从程序的标准输出中读取的。当我从线程中执行myfunc时,它会工作。 But when i execute it in the thread fais..?但是当我在线程中执行它时 fais..? Any suggestion?有什么建议吗?

"Segmentation fault (core dumped)" is the string that Linux prints when a program exits with a SIGSEGV signal and you have core creation enabled. "Segmentation fault (core dumped)"是当程序以SIGSEGV信号退出并且您启用了核心创建时 Linux 打印的字符串。 This means some program has crashed.这意味着某些程序崩溃了。

If you're actually getting this error from running Python, this means the Python interpreter has crashed.如果您确实在运行 Python 时遇到此错误,则意味着 Python 解释器已崩溃。 There are only a few reasons this can happen:发生这种情况的原因只有几个:

  1. You're using a third-party extension module written in C, and that extension module has crashed.您正在使用用 C 编写的第三方扩展模块,并且该扩展模块已崩溃。

  2. You're (directly or indirectly) using the built-in module ctypes , and calling external code that crashes.您(直接或间接)使用内置模块ctypes ,并调用崩溃的外部代码。

  3. There's something wrong with your Python installation.您的 Python 安装有问题。

  4. You've discovered a bug in Python that you should report.您在 Python 中发现了一个应该报告的错误。

The first is by far the most common.第一种是迄今为止最常见的。 If your q is an instance of some object from some third-party extension module, you may want to look at the documentation.如果您的q是来自某个第三方扩展模块的某个对象的实例,您可能需要查看文档。

Often, when C modules crash, it's because you're doing something which is invalid, or at least uncommon and untested.通常,当 C 模块崩溃时,是因为您正在做一些无效的事情,或者至少不常见且未经测试。 But whether it's your "fault" in that sense or not - that doesn't matter.但是,从这个意义上说,这是否是您的“错”——这并不重要。 The module should raise a Python exception that you can debug, instead of crashing.该模块应该引发一个您可以调试的 Python 异常,而不是崩溃。 So, you should probably report a bug to whoever wrote the extension.因此,您可能应该向编写扩展的人报告错误。 But meanwhile, rather than waiting 6 months for the bug to be fixed and a new version to come out, you need to figure out what you did that triggered the crash, and whether there's some different way to do what you want.但与此同时,与其等待 6 个月修复错误并发布新版本,您还需要弄清楚自己做了什么导致了崩溃,以及是否有一些不同的方法可以做你想做的事。 Or switch to a different library.或者切换到不同的库。

On the other hand, since you're reading and printing out data from somewhere else, it's possible that your Python interpreter just read the line "Segmentation fault (core dumped)" and faithfully printed what it read.另一方面,由于您正在从其他地方读取和打印数据,因此您的 Python 解释器可能只是读取了"Segmentation fault (core dumped)"并忠实地打印了它读取的内容。 In that case, some other program upstream presumably crashed.在这种情况下,上游的其他一些程序可能崩溃了。 (It's even possible that nobody crashed—if you fetched this page from the web and printed it out, you'd get that same line, right?) In your case, based on your comment, it's probably the Java program that crashed. (甚至可能没有人崩溃——如果你从网上获取这个页面并打印出来,你会得到同样的一行,对吗?)在你的情况下,根据你的评论,可能是 Java 程序崩溃了。

If you're not sure which case it is (and don't want to learn how to do process management, core-file inspection, or C-level debugging today), there's an easy way to test: After print line add a line saying print "And I'm OK" .如果您不确定是哪种情况(并且今天不想学习如何进行进程管理、核心文件检查或 C 级调试),有一个简单的测试方法:在print line添加一行说print "And I'm OK" If you see that after the Segmentation fault line, then Python didn't crash, someone else did.如果您在Segmentation fault线之后看到,那么 Python 没有崩溃,而是其他人崩溃了。 If you don't see it, then it's probably Python that's crashed.如果你没有看到它,那么很可能是 Python 崩溃了。

There is one more reason for such failure which I came to know when mine failed这种失败还有一个原因,当我的失败时我才知道

  • You might be working with a lot of data and your RAM is full您可能正在处理大量数据并且您的 RAM 已满

This might not apply in this case but it also throws the same error and since this question comes up on top for this error, I have added this answer here.这可能不适用于这种情况,但它也会引发相同的错误,并且由于此问题出现在此错误的首位,因此我在此处添加了此答案。

In my case: I forgot to activate virtualenv就我而言:我忘了激活 virtualenv

I installed "pip install example" in the wrong virtualenv我在错误的 virtualenv 中安装了“pip install example”

It's worth trying faulthandler to identify the line or the library that is causing the issue as mentioned here https://stackoverflow.com/a/58825725/2160809 and in the comments by Karuhanga值得尝试faulthandler来识别导致问题的行或库,如此处https://stackoverflow.com/a/58825725/2160809和Karuhanga的评论中所述

faulthandler.enable()
// bad code goes here

or或者

$ python3 -q -X faulthandler
>>> /// bad cod goes here

In my case I imported pyxlsd module before module wich works with db Mysql.就我而言,我在与 db Mysql 一起使用的模块之前导入了 pyxlsd 模块。 After I did put Mysql module first(upper in code) it became to work like a clock.在我将 Mysql 模块放在首位(代码中的上部)之后,它变得像时钟一样工作。 Think there was some namespace issue.认为存在一些命名空间问题。

Mildly unrelated to the question, but since this page appears whenever you search "(core dumped) python" then I might share a common problem that causes this error.与问题略微无关,但由于每当您搜索“(核心转储)python”时都会出现此页面,因此我可能会分享导致此错误的常见问题。

OpenCV cv2.imshow() sometimes raises this error on servers without graphical interfaces.

Hope I helped.希望我有所帮助。 Have a good day!祝你有美好的一天!

In my case, I was importing load_workbook from openpyxl before importing mysql.connector.就我而言,我在导入 mysql.connector 之前从 openpyxl 导入了 load_workbook。 I just switch the order and it started working again.我只是改变了顺序,它又开始工作了。

Segmentation fault (core dumped) error分段错误(核心转储)错误

from openpyxl import load_workbook
import mysql.connector

Solution解决方案

import mysql.connector
from openpyxl import load_workbook

I faced the same issue while using Python and Pytorch. The problem in my case was old checkpoints.我在使用 Python 和 Pytorch 时遇到了同样的问题。我的问题是旧的检查点。 Due to old checkpoints, Pytorch was not able to load or create new graphs.由于旧检查点,Pytorch 无法加载或创建新图表。 After deleting old checkpoints, the problem was solved.删除旧的检查点后,问题就解决了。

Based on one of the comments found on this post: 基于此帖子中发现的评论之一:

https://bugs.launchpad.net/ubuntu/+source/python-librabbitmq/+bug/1353269 https://bugs.launchpad.net/ubuntu/+source/python-librabbitmq/+bug/1353269

do the following: 请执行下列操作:

removing python-librabbitmq 删除python-librabbitmq

 sudo apt-get remove python-librabbitmq

install librabbitmq 安装librabbitmq

sudo pip install librabbitmq

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

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