我需要从仪器中获取数据并对其进行2D绘图。我可以在没有线程的情况下完成它,但它很慢......我怎么能在没有像#34; QObject :: setParent这样的错误的情况下继续:不能设置父级,新父级在不同的线程中#34;," QApplication:对象事件过滤器不能在不同的线程中#34;和#34; QPixmap:在GUI线程之外使用pixmaps并不安全"。我使用此代码作为测试:
import threading
import time
import numpy as np
import matplotlib.pyplot as plt
class myThread (threading.Thread):
def __init__(self, threadID, name, counter):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.counter = counter
s=(6,6)
self.x=np.zeros(s)
def run(self):
print "Starting " + self.name
threadLock.acquire()
if self.threadID==1:
acquire_data(self.x)
count_to_three(self.name,1)
plot_it(self.x)
count_to_three(self.name,1)
print "Exiting "+ self.name
if self.threadID==2:
count_to_three(self.name,1)
print "Exiting "+ self.name
threadLock.release()
def print_time(threadName, delay, counter):
while counter:
time.sleep(delay)
print "%s: %s" % (threadName, time.ctime(time.time()))
counter -= 1
def count_to_three(threadName,delay):
i=0
while i<4:
time.sleep(delay)
print"%s: %s" % (threadName,i)
i+=1
def acquire_data(x):
return
def plot_it(x):
sidex = np.linspace(0,6,6)
sidey = np.linspace(0,6,6)
X,Y = np.meshgrid(sidex,sidey)
plt.subplot(111)
plt.pcolormesh(X,Y,x)
return
# Create new threads
plt.figure()
threadLock=threading.Lock()
while True:
threads=[]
thread1 = myThread(1, "Thread-1", 3)
thread2 = myThread(2, "Thread-2", 2)
# Start new Threads
thread1.start()
thread2.start()
# Add threads to thread list
threads.append(thread1)
threads.append(thread2)
for t in threads:
plt.show()
t.join()