簡體   English   中英

Python暫停或停止實時數據

[英]Python pause or stop realtime data

這次,我想知道針對以下情況存在哪些可能的解決方案:我讓我的筆記本電腦使用我已經在此處發布的python腳本讀取了原始鼠標數據(Ubuntu OS)。 它具有讀取鼠標文件並從中提取x,y數據的方法。 一會兒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繪制數據,此方法雖然有效,但仍可用於將來的改進;)

這是標准線程模塊的示例-我實際上不知道它的響應速度。 另外,如果您要基於全局熱鍵暫停或開始輸入,而不是從腳本開始輸入,則這取決於您的桌面環境-我只使用了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之后缺少()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM