hans vasquez morales
hans vasquez morales

Reputation: 11

Combine threads

I'm making a program that reads a UDP socket then decodes this information by STEIM1 function, I want it to plot that information on PyQwt but when I try to upload the information to graph the error (Core Dumped) appears. I searched for information on the web but I would like some support, especially for the part of the graph.

#!/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()

Upvotes: 0

Views: 60

Answers (1)

three_pineapples
three_pineapples

Reputation: 11869

Your application is crashing because you are plotting from a thread. You cannot interact with a PyQt GUI from within a thread. Interaction with the GUI (this includes PyQwt) must be done in the main thread only.

Refactoring your code to remove the plotting thread is probably beyond the scope of a stack overflow answer. However, if you run into a specific problem when removing your plotting thread, posting a new question (with details on that problem) on stack overflow is encouraged.

Upvotes: 1

Related Questions