1

I have a PySide2 Application that works in windows. What it does is that it opens a network packet (pcap) file using the python pyshark library inside a QThread class. It is able to open that pcap file but when i tried to run the same app on ubuntu, it throws an error. RuntimeError: Cannot add child handler, the child watcher does not have a loop attached.

I have searched online for solutions and stumbled across this site -> https://github.com/KimiNewt/pyshark/issues/303

It seems that pyshark can only run in the main thread and not in the sub-thread. I have also created a minimal example that replicates my problem as shown below.

import sys
import pyshark
import asyncio
from PySide2.QtCore import QThread, Qt, Slot
from PySide2.QtWidgets import QApplication, QMainWindow, QFrame, QHBoxLayout, QPushButton


class SubThread(QThread):
    def __init__(self):
        super(SubThread, self).__init__()
        self.Input_file = "profibus.pcap"

    def run(self):
        print("thread is running!")
        cap = pyshark.FileCapture(self.Input_file)
        iter_obj = iter(cap)
        pkt = next(iter_obj)
        print(pkt)
        cap.close()

class TopLayout(QFrame):
    def __init__(self, sub_thread):
        self.frame = QFrame()
        self.layout = QHBoxLayout()
        self.sub_thread = sub_thread

        self.toggle_button_box = QHBoxLayout()
        self.toggle_button = QPushButton("Start")
        self.toggle_button.setCheckable(True)
        self.toggle_button_box.addWidget(self.toggle_button)

        self.layout.addLayout(self.toggle_button_box)
        self.layout.setAlignment(Qt.AlignCenter)

        self.frame.setLayout(self.layout)
        self.toggle_button.clicked.connect(self.on_toggle_button_clicked)



    @Slot()
    def on_toggle_button_clicked(self):
        self.sub_thread.start()


class MainWindow(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        self.SubThread = SubThread()
        self.top_layout = TopLayout(self.SubThread)

        self.setCentralWidget(self.top_layout.frame)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()

    # Execute application
    sys.exit(app.exec_())


My current environment configs are:

  • Python 3.7,
  • Pyside2 5.13.2,
  • Pyshark 0.4.2.9 (latest)
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Lee Sai Mun
  • 140
  • 3
  • 13

0 Answers0