简体   繁体   中英

Python: Sharing continuous data between threads

So I'm trying to share data between two threads in a continuous way. The program should keep running until asked to stop. The problem I have is with the queue. Normally it should be inside the while loop in the get_ratio function. But when I put it there I get the error: 'numpy.complex128' object has no attribute 'get'. Also when I put it before the while loop it treats only part of the data because it's before the loop. I don't know how to keep getting data from the queue. Any ideas?

import time
import random
import threading as th
import Queue
import numpy as np
import matplotlib.pyplot as plt
import bluetooth
import scipy.fftpack
from MindwaveDataPoints import RawDataPoint
from MindwaveDataPointReader import MindwaveDataPointReader


def get_data(q):
    mindwaveDataPointReader = MindwaveDataPointReader()
    mindwaveDataPointReader.start()

    data=[]
    while(1):


        dataPoint = mindwaveDataPointReader.readNextDataPoint()

        if (dataPoint.__class__ is RawDataPoint):
            data.append(dataPoint)
            q.put(data) #data keeps going into the queue







def get_ratio(q):

    M = [[],[],[],[],[],[],[],[]]
    fft_avg = []
    fft_avg_mod = []
    #loop to cover all the data
    j=0
    k=0
    l=0
    data = q.get() #HERE 

    while(1):
        #data = q.get() #HERE
        if k > 7 :
            fft_avg[:] = []
            fft_avg_mod[:] = []
            fft_avg = [(x+y+z+t+u+v+w+q)/8 for x,y,z,t,u,v,w,q in zip(M[0],M[1],M[2],M[3],M[4],M[5],M[6],M[7])]
            fft_avg_mod = [ abs(x) for x in fft_avg]


        if fft_avg_mod:
            d=random.randint(8,14)
            e=random.randint(14,20)
            alpha=fft_avg_mod[d]
            beta=fft_avg_mod[e]
            ratio=beta/alpha
            print 'ratio' , ratio
        f=k%8
        M[f] = np.fft.fft(data[j:j+32])
        j = j + 32  
        k = k + 1


if __name__ == '__main__':

    q = Queue.Queue(maxsize=0)

    try:
       threadLock = th.Lock()
       threads=[]
       thread1=th.Thread( target=get_data, args=(q,))
       thread2=th.Thread( target=get_ratio, args=(q,))
       thread1.start()
       print 'T1 started'
       time.sleep(10)
       thread2.start()
       print 'T2 started'
       threads.append(thread1)
       threads.append(thread2)
       for t in threads:
            t.join()
    except:
       print "Error: unable to start thread"

This has nothing to do with threads or sharing data. You are overwriting the data in variable q :

while(1):
    data = q.get()

and then:

        fft_avg = [(x+y+z+t+u+v+w+q)/8 for x,y,z,t,u,v,w,q in zip(M[0],M[1],M[2],M[3],M[4],M[5],M[6],M[7])]

The last line assigns a different value to q in the list comprehension, so you get an error the next time q.get() is called.


Advice 1: As a general rule, whenever you get an error of type XXX object has no attribute YYY , it is probably the case that your variable has completely wrong data (ie not the type you expect).

Advice 2: don't be lazy and use variable names like x , y , z , p , q , r , a , b , c . Give them real names. You will have less errors.

You are using q inside the list comprehension, and therefore overwriting the queue q . Better use speaking variable names, so that such errors are less likely.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM