在Python中的分割错误? [英] Segmentation fault in python?

查看:179
本文介绍了在Python中的分割错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用gtk3创建GUI。为了使GUI和操作一起工作,我使用以下代码创建一个线程: threading.Thread(target = function).start()。没有线程,一切运行良好,但GUI将被冻结。使用线程,发生此错误:

第一个是分段错误(核心转储)



第二个是 *** glibc detected *** python:double free or corruption(!prev):0x09154320 ***

第三个是 Gtk:ERROR:/build/buildd/gtk+3.0-3.4.2 /./ gtk / gtktextview.c: 3726:gtk_text_view_va lidate_onscreen:断言失败:(priv-> onscreen_validated)中止(核心转储)



你知道为什么会发生这种情况?
$ b

编辑:
我的代码:



GUI.py

  from gi.repository import Gtk,Gdk,GLib 
import Process
import gobject
导入线程
$ b $ class gui():
def __init __(self):
self.window = Gtk.Window()
self.window.connect('delete -event',Gtk.main_quit)

self.box = Gtk.Box()
self.window.add(self.box)

self.label = Gtk.Label('idle')
self.box.pack_start(self.label,True,True,0)

self.progressbar = Gtk.ProgressBar()
self.box.pack_start(self.progressbar, True,True,0)

self.button = Gtk.Button(label ='Start')
self.button.connect('clicked',self.on_button_clicked)
self.box.pack_start(self.button,True,True,0)

self.window.show_all()

GLib.threads_init()
Gdk。 threads_init()
Gdk.threads_enter()
Gtk.main()
Gdk.threads_leave()

def working1(self):
self。 label.set_text('working1')
result = Process.heavyworks1()
打印结果
self.label.set_text('idle')

def on_button_clicked(自我,widget):
threading.Thread(target = self.working1).start()

if __name__ =='__main__':
gui = gui()

gui http://i42.t inypic.com/33nvrx2.png

Process.py

a = 0
x ='something'

  def heavyworks1():
#global a
#doing something
#做其他事情
#a =东西
#返回结果
$ b $ def def heavyworks2(param):
#做某事
#做其他事情
#return result


解决方案

这个。你需要做的是从任何线程完全分离任何GTK电话



这些错误发生是因为还有一些代码访问gtk ui工作者线程(在后台执行我的计算的线程)。我需要做的仅仅是通过使用 gobject.idle_add(some_fuction_telling_gtk_what_to_do)
$与线程分开的 ALL gtk调用b $ b

这是一个样本:

  def stop_progress(self):
self.worker.join ()
self.label.set_text('idle')

def working_thread(self,num):
self.textview.set_text(num)
过程。 heavyworks2(num)
gobject.idle_add(self.stop_progress)

self.worker = threading.Thread(target = self.working_thread,args = [100000])
self。 worker.start()

您从该代码中看到一个函数,它假定在后台工作( working_thread(self,num))仍然有一行访问gtk调用( self.textview.set_text(num))。将该代码分离成一个函数,并使用 gobject.idle_add(gtk_call_function)从您的线程中调用它。



  def stop_progress(self):
self.worker.join()
self。 label.set_text('idle')

def updateTextView(self,num):
self.textview.set_text(num)

def working_thread(self,num ):
gobject.idle_add(self.updateTextView,num)
Process.heavyworks2(num)
gobject.idle_add(self.stop_progress)

self.worker = threading.Thread(target = self.working_thread,args = [100000])
self.worker.start()

所以,这里的一个重点是不要直接从任何线程更新gtk ui。只需将访问gtk的每个代码分离为一个函数,并通过 gobject.idle_add()从线程调用它。


I'm creating GUI using gtk3. So that the GUI and operation works together, I make a thread with this code: threading.Thread(target=function).start(). Without threading, everything works well, but the GUI will be freezed. With threading, this error occured:

The first one is Segmentation fault (core dumped)

The second one is *** glibc detected *** python: double free or corruption (!prev): 0x09154320 ***

The third one is Gtk:ERROR:/build/buildd/gtk+3.0-3.4.2/./gtk/gtktextview.c:3726:gtk_text_view_va‌​lidate_onscreen: assertion failed: (priv->onscreen_validated) Aborted (core dumped)

Do you know why did that happens?

EDIT: my code:

GUI.py

from gi.repository import Gtk, Gdk, GLib
import Process
import gobject
import threading

class gui():
def __init__(self):
    self.window = Gtk.Window()
    self.window.connect('delete-event', Gtk.main_quit)

    self.box = Gtk.Box()
    self.window.add(self.box)

    self.label = Gtk.Label('idle')
    self.box.pack_start(self.label, True, True, 0)

    self.progressbar = Gtk.ProgressBar()
    self.box.pack_start(self.progressbar, True, True, 0)

    self.button = Gtk.Button(label='Start')
    self.button.connect('clicked', self.on_button_clicked)
    self.box.pack_start(self.button, True, True, 0)

    self.window.show_all()

    GLib.threads_init()
    Gdk.threads_init()
    Gdk.threads_enter()
    Gtk.main()
    Gdk.threads_leave()

def working1(self):
   self.label.set_text('working1')
   result = Process.heavyworks1()   
   print result
   self.label.set_text('idle')  

def on_button_clicked(self, widget):
    threading.Thread(target=self.working1).start()

if __name__ == '__main__':
    gui = gui()

gui http://i42.tinypic.com/33nvrx2.png

Process.py

a = 0 x = 'something'

def heavyworks1():
    #global a
    #doing something
    #doing other thing
    #a = something
    #return result

def heavyworks2(param):
     #doing something
    #doing other thing
    #return result

解决方案

I've resolved this one. What you need to do is FULLY SEPARATE ANY GTK CALL FROM ANY THREAD.

Those error occured because there were still some code accessing gtk ui from worker thread (thread that doing my calculation in the background). What I need to do is just separate ALL gtk call from thread by using gobject.idle_add(some_fuction_telling_gtk_what_to_do)

This is a sample:

def stop_progress(self):
    self.worker.join()
    self.label.set_text('idle') 

def working_thread(self, num):
    self.textview.set_text(num)
    Process.heavyworks2(num)    
    gobject.idle_add(self.stop_progress)

self.worker = threading.Thread(target=self.working_thread, args=[100000])
self.worker.start()

You see from that code a function that suppose to working in the background (working_thread(self,num)) still having a line accessing gtk call (self.textview.set_text(num)). Separate that code into a function and call it from your thread using gobject.idle_add(gtk_call_function).

It's become like this.

def stop_progress(self):
    self.worker.join()
    self.label.set_text('idle') 

def updateTextView(self, num):
    self.textview.set_text(num)     

def working_thread(self, num):
    gobject.idle_add(self.updateTextView, num)
    Process.heavyworks2(num)    
    gobject.idle_add(self.stop_progress)

self.worker = threading.Thread(target=self.working_thread, args=[100000])
self.worker.start()

So, one important point here is don't update gtk ui directly from any thread. Just separate every code accessing gtk into a function and call it by gobject.idle_add() from thread.

这篇关于在Python中的分割错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆