我正在尝试使用Raspberry PI在LED标牌上显示RSS数据。我的代码基于首次购买该标志时发现的脚本。这是一个简单的脚本,可让您向标牌发送消息和颜色,它将滚动直到键盘中断为止。
sudo python scroll "Hello World" 1 #red
sudo python scroll "Hello World" 2 #green
sudo python scroll "Hello World" 3 #red and green (orange)
此脚本与我正在处理的脚本之间的区别在于,所有数据都在循环之前处理,然后将showmatrix()函数用于在屏幕上显示字符串,并使用shiftmatrix()函数来处理滚动图像。
为了不断下载RSS数据,我将以下代码放入循环中:
#grab emails
newmails = int(feedparser.parse("https://" + USERNAME + ":" + PASSWORD +"@mail.google.com/gmail/feed/atom")["feed"]["fullcount"])
textinput = "You have " + str(newmails) + " new emails"
# append extra characters to text input to allow for wrap-around
textinput+=" :: "
然后,我使用与以前相同的功能在标牌上显示此数据:
# Continually output to the display until Ctrl-C
#
# loop around each column in the dotarray
for col in range(len(dotarray[0])):
for row in range(8):
# copy the current dotarray column values to the first column in the matrix
matrix[row][0]=(dotarray[row][col])
# now that we have updated the matrix lets show it
showmatrix()
# shift the matrix left ready for the next column
shiftmatrix()
由于RSS数据下载需要花费很长时间(至少一秒钟),因此该时间段内不会运行输出循环,并且符号会变为空白。有没有一种可以同时运行feedparser函数的方式,所以没有延迟?
我认为多线程是前进的方向,对吗?我查看了couroutines,但那无济于事。
是的,os.fork(),可以使函数在其他进程中运行,也可以使线程模块在其他线程中运行。如果函数使用全局变量,则需要使用线程模块并使其在另一个线程中运行,如果我不建议这样做,则资源浪费较少(假设函数未分配大量内存或以其他方式使用大量资源) ),您的代码应如下所示:
class displayThread(threading.Thread)
*init function if you need to pass info to the tread, otherwise dont write one but if you do
make sure to call Thread.__init__() first in your function*
def run(): //Overrides the run function
*display what you want on the display*
class downloadThread(threading.Thread)
*init function if you need to pass info to the tread, otherwise dont write one but if you do
make sure to call Thread.__init__() first in your function*
def run(): //Overrides the run function
*download what you want*
并且您的主脚本应如下所示:
thread1 = displayThread
thread2 = downloadThread
thread1.start()
thread2.start()
thread2.join() //waits for download to finish while the display in being updated by the other thread
如果要停止显示线程(假设它永远存在),则必须添加以下内容:
os.kill(thread1.getpid(), signal.SIGKILL)
.join()之后,然后对下载的信息进行所需的操作。
多进程版本非常相似,如果遇到问题,您应该能够从我的示例和os.fork()文档中了解如何制作它-让我知道,我将对其进行编辑。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句