结合线程

时间:2014-11-19 14:58:20

标签: python multithreading pyqt

我正在创建一个读取UDP套接字的程序,然后通过STEIM1函数对此信息进行解码,我希望它在PyQwt上绘制该信息,但是当我尝试上传信息以绘制错误时(Core Dumped) )出现。我在网上搜索了一些信息,但我想得到一些支持,特别是对于图表的部分。

#!/usr/bin/env python
import random, sys
from PyQt4 import QtGui, QtCore
import socket
import threading
import Queue
from datetime import datetime, date, time
import milibreria
from PyQt4.Qwt5 import *

host = '0.0.0.0'
port = 32004
buffer = 1024
my_queue = Queue.Queue()####################################################
my_queue1= Queue.Queue()

class readFromUDPSocket(threading.Thread):

    def __init__(self, my_queue): 
        threading.Thread.__init__(self)
        self.setDaemon(True)
        self.my_queue = my_queue

    def run(self):
        while True:
            buffer1,addr = socketUDP.recvfrom(buffer)
            self.my_queue.put(buffer1)
            print 'UDP received'

class process(threading.Thread):

    def __init__(self, my_queue,my_queue1):
        threading.Thread.__init__(self)
        self.setDaemon(True)
        self.my_queue = my_queue
        self.my_queue1 = my_queue1
        self.alive = threading.Event()
        self.alive.set()

    def run(self):
        while True:
            buffer3 = self.my_queue.get()
            le=len(buffer3)
            #today = datetime.now()
            #timestamp = today.strftime("%A, %B %d, %Y %H:%M:%S")
            #print 'Dato recibido el:', timestamp
            DTj,le=milibreria.STEIM1(buffer3)
            self.my_queue1.put(DTj)

class Plot(threading.Thread):

    def __init__(self, my_queue1):
        threading.Thread.__init__(self)
        self.setDaemon(True)
        self.my_queue1 = my_queue1
        self.alive = threading.Event()
        self.alive.set()

    def run(self):
        while True:
            Datos = self.my_queue1.get()
            print Datos,len(Datos)
            milibreria.plotmat(Datos)

if __name__ == '__main__':
    # Create socket (IPv4 protocol, datagram (UDP)) and bind to address
    socketUDP = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    socketUDP.bind((host, port))
    # Instantiate & start threads
    myServer   = readFromUDPSocket(my_queue)
    #mySerial  = readFromSerial(my_queue)
    myDisplay  = process(my_queue,my_queue1)
    myPlot = Plot(my_queue1)
    myServer.start()
    myDisplay.start()
    #mySerial.start()
    myPlot.start()
    #plotThread = threading.Thread(target=main)
    #plotThread.start()

while 1:
    pass

UDPSock.close()

1 个答案:

答案 0 :(得分:1)

您的应用程序崩溃了,因为您正在从一个帖子中进行绘图。您无法在线程内与PyQt GUI进行交互。与GUI的交互(包括PyQwt)必须仅在主线程中完成。

重构代码以删除绘图线程可能超出了堆栈溢出答案的范围。但是,如果在删除绘图线程时遇到特定问题,则鼓励在堆栈溢出时发布新问题(包含该问题的详细信息)。