通过Python从Android访问LogCat [英] Accessing LogCat from Android via Python
本文介绍了通过Python从Android访问LogCat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以读取通过python在LogCat上发送的信息?
Is it possible to read information being sent over LogCat in python?
我有一个用Java编写的程序.
每个并条机都会发送标签:"Fps:"消息:数字
我希望此消息触发一个事件,该事件可以在python脚本中捕获,因此可以绘制fps-meter.
I have a program that is written in java.
Every draw frame it sends tag:"Fps: " message: number
I would like this message to fire an event that I can catch in my python script so I can draw a fps-meter.
推荐答案
看看 subprocess .以下代码改编自 Stefaan Lippens
Take a look at subprocess. The following code was adapted from Stefaan Lippens
import Queue
import subprocess
import threading
class AsynchronousFileReader(threading.Thread):
'''
Helper class to implement asynchronous reading of a file
in a separate thread. Pushes read lines on a queue to
be consumed in another thread.
'''
def __init__(self, fd, queue):
assert isinstance(queue, Queue.Queue)
assert callable(fd.readline)
threading.Thread.__init__(self)
self._fd = fd
self._queue = queue
def run(self):
'''The body of the tread: read lines and put them on the queue.'''
for line in iter(self._fd.readline, ''):
self._queue.put(line)
def eof(self):
'''Check whether there is no more content to expect.'''
return not self.is_alive() and self._queue.empty()
# You'll need to add any command line arguments here.
process = subprocess.Popen(["logcat"], stdout=subprocess.PIPE)
# Launch the asynchronous readers of the process' stdout.
stdout_queue = Queue.Queue()
stdout_reader = AsynchronousFileReader(process.stdout, stdout_queue)
stdout_reader.start()
# Check the queues if we received some output (until there is nothing more to get).
while not stdout_reader.eof():
while not stdout_queue.empty():
line = stdout_queue.get()
if is_fps_line(line):
update_fps(line)
当然,您需要自己编写is_fps_line
和update_fps
函数.
Of course, you'll need to write the is_fps_line
and update_fps
functions yourself.
这篇关于通过Python从Android访问LogCat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文