简体   繁体   English

Python:在线程之间共享连续数据

[英]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. 通常,它应该在get_ratio函数的while循环内。 But when I put it there I get the error: 'numpy.complex128' object has no attribute 'get'. 但是当我把它放在那里时,我得到了一个错误:“ numpy.complex128”对象没有属性“ get”。 Also when I put it before the while loop it treats only part of the data because it's before the loop. 同样,当我将其放在while循环之前时,它只处理部分数据,因为它在循环之前。 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 : 您正在覆盖变量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. 最后一行为列表理解中的q分配了一个不同的值,因此,下次调用q.get()时会出错。


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). 建议1:通常,每当您收到类型为XXX object has no attribute YYY的错误的XXX object has no attribute YYY ,很可能是您的变量具有完全错误的数据(即不是您期望的类型)。

Advice 2: don't be lazy and use variable names like x , y , z , p , q , r , a , b , c . 建议2:不要懒惰,并使用xyzpqrabc等变量名。 Give them real names. 给他们起真实的名字。 You will have less errors. 您将减少错误。

You are using q inside the list comprehension, and therefore overwriting the queue q . 您在列表推导中使用q ,因此覆盖了队列q Better use speaking variable names, so that such errors are less likely. 最好使用有说服力的变量名,以免发生此类错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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