2024-06-28 19:30:28 发布
网友
我在一个程序中使用Pyserial来做一个RS232的串行接口。它已经工作了,但问题是我的设备,奴隶可以通信任何时候,并解决我的代码我用
while(ser.inWaiting() == 0): #wait for some input the read it ser.readline()
问题是Python进程在我的CPU上过于庞大。所以我的问题是:我怎么能做得更“轻”,没有所有的过程?你知道吗
以我的经验,PySerial太重,无法传输大数据流。最好放弃抽象,使用普通的系统调用。你知道吗
在我的应用程序中,用PySerial以50千波特的速度传输使用了i5-3550内核的70-80%。后来,使用普通的tcsetattr()、open()、write()用C重写的应用程序在Raspberry Pi中运行,没有明显的CPU使用率(如<;1%)。你知道吗
tcsetattr()
open()
write()
即使在Python上使用带有os和termios模块的系统调用,甚至可能使用CTypes来调用cfsetospeed()和cfsetispeed(),都将提供巨大的加速。你知道吗
os
termios
cfsetospeed()
cfsetispeed()
你可以使用线程。下面是一个非常简单的例子:
#!/usr/bin/env python import threading import time import sys from subprocess import Popen, PIPE import os running = False def Task1(): time.sleep(5) count = 0 while running: count += 1 print "Task1 - count is", count time.sleep(3) def Task2(): while running: x = raw_input("Enter some text:") print "you entered:", x def Task3(): pipe = os.popen('./generate') while running: a = pipe.readline() a = a.strip('\n') print "Task3: ", a t1 = threading.Thread(target = Task1, args=[]) t2 = threading.Thread(target = Task2, args=[]) t3 = threading.Thread(target = Task3, args=[]) running = True # t2.start() t1.start() t3.start() time.sleep(20) print "=== exiting ===" running = False t1.join() t3.join() sys.exit(0)
generate程序只是(注意它使用了-u):
generate
-u
#!/usr/bin/env python -u import time import sys count = 0 while True: count += 1 print "generate count is", count time.sleep(2)
以我的经验,PySerial太重,无法传输大数据流。最好放弃抽象,使用普通的系统调用。你知道吗
在我的应用程序中,用PySerial以50千波特的速度传输使用了i5-3550内核的70-80%。后来,使用普通的
tcsetattr()
、open()
、write()
用C重写的应用程序在Raspberry Pi中运行,没有明显的CPU使用率(如<;1%)。你知道吗即使在Python上使用带有
os
和termios
模块的系统调用,甚至可能使用CTypes来调用cfsetospeed()
和cfsetispeed()
,都将提供巨大的加速。你知道吗你可以使用线程。下面是一个非常简单的例子:
generate
程序只是(注意它使用了-u
):相关问题 更多 >
编程相关推荐