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"
datawhich is growing toqrepeatedly. Is this what you want? Why don't you just putdataPointintoq?