Important: Please read the Qt Code of Conduct -

PySide, pySerial and threading causes segfault

  • I'm trying to use PySide and pySerial to make a cross-platform app that interacts with the serial port. I originally used Qtimers to poll the serial for data, but this put a large load on the cpu. So I've been attempting to use threads instead.

    unfortunately using threads causes either Qt or pySerial to Segfault.

    I've tried both python threads and QThreads, same problem, it happens on OSX, windows 8 and Ubuntu 12.04. using python 2.7 and Qt4

    "This question seemed to have a similar problem,":

    "this thread also seems to be a similar problem":

    below is a small app that recreates the problem

    #! /usr/bin/python
    import sys
    import serial
    import threading
    from PySide import QtCore, QtGui, QtUiTools

    class serial_port_class(object):
    def init(self):
    self.connected = False

    def __del__(self):
    def connect_port(self):
            self.serial_port = serial.Serial("/dev/tty.usbmodem1451", 9600, timeout = None)
            self.connected = True
        except serial.SerialException, e:
            self.connected = False
        if self.connected:
            self.serial_thread = threading.Thread(target=self.recieve_port, args=(self))
    def disconnect_port(self):
        self.connected = False
    def recieve_port(self):
        while self.connected:
                text =
                if text != '':
                    print "text"
            except serial.SerialException, e:
                connected = False

    class KeyPressEater(object):
    def connect_port(self):
    self.serial_thread = threading.Thread(target=self.eventFilter)

    def eventFilter(self):
        global serial_port
        while serial_port.connected == True:
            ch = raw_input()
            if serial_port.connected == True:
            return QtCore.QObject.eventFilter(self, obj, event)

    def main():
    global serial_port
    serial_port = serial_port_class()
    heyeater = KeyPressEater()

    if serial_port.connected== True:

    if name == 'main':


    sometimes it takes a bit of data IO to trigger the segfault, sometimes a single character will trigger it...

    I'm not really sure how I would go about debugging this further....

    All help is appreciated!!!!!!

Log in to reply