多次写入文件
我有以下Python代码,该代码期望数据来自串行端口,并将其写入文件。
import time
import serial
def write_log ( text ):
f = open('logger.log', 'a')
f.write( text )
f.close()
ser = serial.Serial()
ser.port = "/dev/ttyS0"
ser.baudrate = 4800
ser.open()
if ser.isOpen():
while 1:
while ser.inWaiting() <= 0:
time.sleep(1)
response = ser.read(ser.inWaiting())
if len ( response ):
write_log( response )
print response
它在一定程度上起作用,因为一段时间后,它开始挂起,使 CPU 一路上升,并且不向 .log 文件写入任何内容(或者有时只写入文本片段)。
这里的过程非常密集,因为我的串口每秒都会写入一个 8 字节的字符串,然后这个 python 脚本应该接收它,并将其内容写入日志文件。
我认为这里的问题是我正在打开和关闭文件太多,这在某种程度上使整个过程慢了。 I'm no python wizz, so any help or advice on improving this code would be greatly appreciated.
提前致谢,
I have the following python code that expects data coming from the serial port, and writes it to a file.
import time
import serial
def write_log ( text ):
f = open('logger.log', 'a')
f.write( text )
f.close()
ser = serial.Serial()
ser.port = "/dev/ttyS0"
ser.baudrate = 4800
ser.open()
if ser.isOpen():
while 1:
while ser.inWaiting() <= 0:
time.sleep(1)
response = ser.read(ser.inWaiting())
if len ( response ):
write_log( response )
print response
It works to an extent, as after some time, it starts to hang, bringing the CPU all the way up, and not writing anything (or sometimes writing only pieces of text) to the .log file.
The process here is pretty intensive, as my serial port will be writing an 8 bytes string every second, and this python script is supposed to then receive it, and write its contents to the log file.
I'm thinking the problem here is the fact that I'm opening and closing the file too much, and this is somehow making the whole process slow. I'm no python wizz, so any help or advice on improving this code would be greatly appreciated.
Thanks in advance,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的代码中有一个无限的循环,并且在出现问题时不会脱离它 - 否则串行设备不再打开。
可能使用:
什至:
我也不确定睡眠;您最好只等待阅读以获取可用的数据。
正如我所考虑的那样,不知道串行类中可用的方法,我认为主循环应该尝试从串行设备读取,如果当前没有任何可用的话,请愉快地悬挂,并且仅在输入方法指示的时候终止不再需要输入 - 该设备已关闭,或者以某种方式失败。
You have an infinite loop in your code, and you don't break out from it when there is a problem - or the serial device is no longer open.
Probably use:
or even:
I'm not sure about the sleep, either; you'd do better just waiting in the read for data to become available.
As I think about it, not knowing the methods available in the serial class, the more I think the main loop should be attempting to read from the serial device, hanging happily if there is nothing currently available, and only terminating when the input method indicates there is no more input to come - the device has been closed on you, or has failed in some way.
我认为这里的问题是您正在轮询更新。
Python 的
serial.read()
函数实际上会阻塞当前线程,直到线程上的某些内容变得可读为止,直到超时。因此,你应该做的是打破另一个线程来处理串行 IO。让它无限循环,检查条件(主线程希望您保持监听并且串行端口仍然可用)。该线程将执行以下操作:然后,当您希望它退出时,您的主线程会设置
thisthread_should_keep_doing_this=False
,当您的从属线程完成读取时,它会自行终止。两件事:
请参阅http://pyserial.sourceforge.net/examples.html#miniterm
I think the problem here is that you are polling for updates.
Python's
serial.read()
function actually blocks the current thread until something becomes readable on the thread, until timeout. What you should do, therefore, is break out another thread to handle serial IO. Have it loop indefinitely, checking a condition (the master thread wants you to stay listening and the serial port is still available). This thread will do something like:Then, when you want it to exit, your master thread sets
thisthread_should_keep_doing_this=False
and when your slave thread has finished reading, it kills itself.Two things:
See http://pyserial.sourceforge.net/examples.html#miniterm