Python暂停或停止实时数据
这一次我想知道对于以下情况有什么可能的解决方案:我让我的笔记本电脑用我已经发布在这里的python脚本读取Ubuntu OS上的原始鼠标数据。它有一个方法,读取鼠标文件并从中提取x,y数据。while-true循环使用此方法将数据放入数组。当我在一段时间后使用时间计数器停止读取时,脚本会将数据放入excel文件中。 我现在需要的是暂停数据流的选项,即在不创建数据的情况下更改鼠标位置,然后恢复数据流。我想要一些东西来停止阅读并将其写入excelPython暂停或停止实时数据,python,stream,export-to-excel,Python,Stream,Export To Excel,这一次我想知道对于以下情况有什么可能的解决方案:我让我的笔记本电脑用我已经发布在这里的python脚本读取Ubuntu OS上的原始鼠标数据。它有一个方法,读取鼠标文件并从中提取x,y数据。while-true循环使用此方法将数据放入数组。当我在一段时间后使用时间计数器停止读取时,脚本会将数据放入excel文件中。 我现在需要的是暂停数据流的选项,即在不创建数据的情况下更改鼠标位置,然后恢复数据流。我想要一些东西来停止阅读并将其写入excel import struct import matpl
import struct
import matplotlib.pyplot as plt
import numpy as np
import xlsxwriter
import time
from drawnow import *
workbook = xlsxwriter.Workbook('/path/test.xlsx')
worksheet = workbook.add_worksheet()
file = open( "/dev/input/mouse2", "rb" );
test = [(0,0,0)]
plt.ion()
def makeFig():
plt.plot(test)
#plt.show()
def getMouseEvent():
buf = file.read(3);
button = ord( buf[0] );
bLeft = button & 0x1;
x,y = struct.unpack( "bb", buf[1:] )
_zeit = time.time()-test[-1][-1]
print ("x: %d, y: %d, Zeit: %d\n" % (x, y, _zeit) )
return x,y, _zeit
zeit = time.time()
warte = 0
while warte < 20:
test.append(getMouseEvent())
warte = time.time()-zeit
row = 1
col = 0
worksheet.write(0,0, 'x-richtung')
worksheet.write('C1', 'Zeit')
for x, y , t in (test):
worksheet.write(row, col, x)
worksheet.write(row, col + 1, y)
worksheet.write(row, col + 2, t)
row += 1
chart = workbook.add_chart({'type': 'line'})
chart.add_series({'values': '=Sheet1!$A$1:$A$'+str(len(test))})
worksheet.insert_chart('D2', chart)
workbook.close()
#drawnow(makeFig)
#plt.pause(.00001)
file.close();
如果有像暂停/取消暂停的命中空间这样的东西,那就太棒了。q代表结束和扑救,但我不知道怎么做。任何想法都很好:
哦,我试着用matplotlib绘制数据,虽然效果不错,但这是为了将来的改进 这里是一个标准线程模块的示例-我不知道它的响应速度有多快。此外,如果您想基于全局热键而不是脚本暂停或启动输入,这将取决于您的桌面环境-我只使用了xlib,但应该有一个python包装器,它在某个地方浮动
import threading
import struct
data =[]
file = open("/dev/input/mouse0", "rb")
e=threading.Event()
def getMouseEvent():
buf = file.read(3);
#python 2 & 3 compatibility
button = buf[0] if isinstance(buf[0], int) else ord(buf[0])
bLeft = button & 0x1;
bMiddle = ( button & 0x4 ) > 0;
bRight = ( button & 0x2 ) > 0;
x,y = struct.unpack( "bb", buf[1:] );
return "L:%d, M: %d, R: %d, x: %d, y: %d\n" % (bLeft,bMiddle,bRight, x, y)
def mouseCollect():
global e
#this will wait while e is False (without breaking the loop)
#and loop while e is True
while e.wait():
#do something with MouseEvent data, like append to an array, or redirect to pipe etc.
data.append(getMouseEvent())
mouseCollectThread = threading.Thread(target=mouseCollect)
mouseCollectThread.start()
#toggle mouseCollect with any keyboard input
#type "q" or "quit" to quit.
while True:
x = input()
if x.lower() in ['quit', 'q', 'exit']:
mouseCollectThread._stop()
file.close()
break
elif x:
e.clear() if e.isSet() else e.set()
编辑:我丢失了一个after e.isSet你能用greenlet之类的东西来线程化数据收集,并用键绑定从数据记录线程调用一个开关吗?到目前为止,我还没有很好地掌握线程化,但我会研究它。一个看起来像什么样的示例会很好:Greenlet有一些文档,其中包含一些可能有用的示例:。另外,将问题集中在一些特定的方面也可能对您有好处,例如停止数据收集,并将绘图和文件相关部分分开。这样可以更容易地了解您的问题所在。这可能是select或的一个好例子。我不确定select或epoll在这方面有何帮助。它似乎有助于检查数据i/o是否正在发生。如果我没看错的话。嗨,谢谢你的帮助!我尝试了代码,但似乎没有从线程中获取数据。我在getmouseevent中输入了一个print cmd,但它没有打印任何内容。我读的是正确的mousefile。有什么建议吗?这取决于您想对数据做什么,在我的示例中,它应该被添加到数据中-确保您的设备/dev/input/mouse是正确的。您可以通过从InteractiveShell运行GetMouseeEvent来测试它,以确保它返回一个结果。仅此方法就提供了数据。但是当在线程中似乎什么都没有发生时,我确实使用了一个print命令来检查是否发生了一些事情,但没有打印。检查e.isSet是否返回True,mouseCollectThread.is_live是否为True。好的,谢谢您的耐心;e、 isSet返回false。mouseCollectThread返回true。