【问题标题】:Python pause or stop realtime dataPython 暂停或停止实时数据
【发布时间】:2015-01-07 15:53:28
【问题描述】:

这次我想知道以下情况存在哪些可能的解决方案:我让我的笔记本电脑使用我已经发布在这里的 python 脚本读取原始鼠标数据(Ubuntu OS)。它有一个方法,可以读取鼠标文件并从中提取 x,y 数据。 while true 循环使用此方法将数据放入数组中。当我在一段时间后使用计时器停止读取时,脚本将数据放入 excel 文件中。 我现在需要的是一个暂停数据流的选项,即在不创建数据的情况下更改鼠标位置,然后恢复它。我想要一些东西来停止阅读并将其写入excel。

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 绘制数据,这很有效,但它是未来改进的东西;)

【问题讨论】:

  • 你能不能用类似 greenlet 的东西来线程化数据收集,并用键绑定从数据记录线程调用 switch()?
  • 目前我还没有很好地掌握线程,但我会研究一下。一个样本看起来会很好:)
  • Greenlet 的文档中有一些可能会有所帮助的示例:greenlet.readthedocs.org/en/latest/#introduction。此外,将您的问题集中在特定的事情上可能对您有好处 - 例如(停止数据收集)并拥有绘图和文件相关部分(分开)它可以更容易地查看您的问题。
  • 这对于selectepoll 来说可能是一个很好的例子。
  • 我不确定 select 或 epoll 在这里有什么帮助。它似乎有助于检查数据 i/o 是否正在发生。如果我没看错的话。

标签: python stream export-to-excel


【解决方案1】:

这是一个标准线程模块的示例 - 我实际上不知道它的响应速度如何。此外,如果您想暂停或开始基于全局热键而不是脚本的输入,这将取决于您的桌面环境——我只使用了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()

编辑:我在 e.isSet 之后缺少()

【讨论】:

  • 您好,感谢您的帮助!我尝试了代码,但似乎我没有从线程中获取数据。我将打印命令放入 getmouseevent() 但它什么也没打印(它是我读取的正确鼠标文件)。有什么建议吗?
  • 这取决于你想对数据做什么,在我的例子中,它应该只是附加到数据中——确保你的设备(“/dev/input/mouse”)是正确的。您可以通过从交互式 shell 运行 getMouseEvent 来测试它,以确保它返回结果。
  • 如上所述,我检查了设备是否正确。并且单独的方法给出了数据。但是当在线程中似乎什么都没有发生时,我确实放了一个打印命令来检查是否发生了一些事情但没有打印任何内容。
  • 检查 e.isSet() 返回 True,mouseCollectThread.is_alive() 是否为 True。
  • 好的,感谢您的耐心等待;) e.isSet() 返回 false。 mouseCollectThread 返回 true。
猜你喜欢
  • 1970-01-01
  • 2010-11-28
  • 2020-05-01
  • 1970-01-01
  • 2019-10-18
  • 2019-07-20
  • 1970-01-01
  • 1970-01-01
  • 2014-04-17
相关资源
最近更新 更多