在python中生成一个包含套接字连接的QThread

发布于 2024-12-02 08:43:42 字数 1827 浏览 0 评论 0原文

我目前正在尝试实现一个包含套接字连接的 QThread。套接字连接重复运行(同时 1:)检查接收到的新数据。一旦接收到该数据,它就会发出一个信号,调用一个函数并向其提供接收到的数据。

我的套接字连接工作了。当我单独运行该函数时,它会等待数据,并在新数据进入时进行打印。但是,由于我尝试使用 Qt 构建 GUI,所以我必须将其放在自己的线程中,以便应用程序能够继续运行。

因此,为了线程化它,我实现了一个 GenericThread 类,它接受任何函数并在线程内运行它。我的 MainWindow 类连接套接字 SIGNAL,实例化一个 GenericThread,然后启动它。然而,这会导致我的应用程序挂起。下面是相关的代码片段:

套接字连接

def remoteConn(self, HOST='my.server', PORT=25562):
  import socket
  s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  s.connect((HOST, PORT))
  newLinesRaw = ''
  while 1:
    newData = s.recv(1024)
    if newData:
      print '<rawData>\n', newData, '\n</newData>\n'
      newLinesRaw += newData
    else:
      if newLinesRaw:
        newLines = newLinesRaw.split('\n')
        print '\nNew Lines:\n', newLines
        self.emit(QtCore.SIGNAL('newRemoteLines'), newLines)
        newLinesRaw=''
      else:
        time.sleep(.1)
  s.close()

通用线程类

class GenericThread(QtCore.QThread):
  def __init__(self, function, *args, **kwargs):
    QtCore.QThread.__init__(self)
    self.function = function
    self.args = args
    self.kwargs = kwargs

  def __del__(self):
    self.wait()

  def run(self):
    if self.args and self.kwargs:
      self.function(*self.args,**self.kwargs)
    elif self.args and not self.kwargs:
      self.function(*self.args)
    elif not self.args and self.kwargs:
      self.function(**self.kwargs)
    else:
      self.function()
    return

生成远程线程。请注意,两个打印语句都被执行。

  print 'spawning remote thread'
  self.connect(self, QtCore.SIGNAL('newRemoteLines'), self.routeServerLines)
  thread = GenericThread(self.remoteConn)
  thread.start()
  print 'thread started'

我对套接字和线程很陌生,所以我可能在某个地方犯了一个非常愚蠢的错误。

I am currently trying implement a QThread that contains a socket connection. The socket connection runs repeatedly (while 1:) checking for new data received. Once this data is received, it is supposed to fire off a SIGNAL calling a function and feeding it the received data.

I got the socket connection working. When I run the function on its own it waits for data and prints whenever new data comes in. However since I am trying to build a GUI with Qt I have to put this in its own thread so it allows the app to continue functioning.

So to thread it I implemented a GenericThread class that takes any function and runs it inside a thread. My MainWindow class connects the sockets SIGNAL, instantiates a GenericThread and then starts it. This however causes my app to hang. Below are the relevant pieces of code:

The socket connection

def remoteConn(self, HOST='my.server', PORT=25562):
  import socket
  s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  s.connect((HOST, PORT))
  newLinesRaw = ''
  while 1:
    newData = s.recv(1024)
    if newData:
      print '<rawData>\n', newData, '\n</newData>\n'
      newLinesRaw += newData
    else:
      if newLinesRaw:
        newLines = newLinesRaw.split('\n')
        print '\nNew Lines:\n', newLines
        self.emit(QtCore.SIGNAL('newRemoteLines'), newLines)
        newLinesRaw=''
      else:
        time.sleep(.1)
  s.close()

The generic thread class

class GenericThread(QtCore.QThread):
  def __init__(self, function, *args, **kwargs):
    QtCore.QThread.__init__(self)
    self.function = function
    self.args = args
    self.kwargs = kwargs

  def __del__(self):
    self.wait()

  def run(self):
    if self.args and self.kwargs:
      self.function(*self.args,**self.kwargs)
    elif self.args and not self.kwargs:
      self.function(*self.args)
    elif not self.args and self.kwargs:
      self.function(**self.kwargs)
    else:
      self.function()
    return

Spawning the remote thread. Note that both print statements are executed.

  print 'spawning remote thread'
  self.connect(self, QtCore.SIGNAL('newRemoteLines'), self.routeServerLines)
  thread = GenericThread(self.remoteConn)
  thread.start()
  print 'thread started'

I am new to sockets and threading so I may be making a very stupid error somewhere.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

远昼 2024-12-09 08:43:42

如果没有进一步引用该线程,则该线程可能会被销毁。

尝试使用 self.thread 而不仅仅是 thread

The thread might be destroyed if there is no further reference to it.

Try using self.thread instead of just thread.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文