简体   繁体   English

Python 在树莓派中运行两个程序时变慢

[英]Python becomes slow when running two programs in raspberry pi

When running two programs that use python gpio pins on the Raspberry Pi, one becomes slower.在 Raspberry Pi 上运行两个使用 python gpio 引脚的程序时,其中一个会变慢。 One is a magnetic sensor, and the other is a temperature and humidity sensor.一个是磁传感器,另一个是温湿度传感器。 The latter is the one that slows down.后者是减速的。 Rather than printing the temperature every 2 seconds, it prints every 5 to 10 seconds.它不是每 2 秒打印一次温度,而是每 5 到 10 秒打印一次。 If it comes in the magnetic sensor operation runs faster.如果它进来磁传感器运行速度更快。

Looks like your problem isn't the Python, but the IO gap. 看来您的问题不是Python,而是IO差距。 Even if you are using different pins, the GPIO from the Raspberry uses the same bus to handle all the pins, so one process need to wait the other uses to than have it's time. 即使您使用的是不同的引脚,Raspberry的GPIO也使用相同的总线来处理所有引脚,因此,一个进程需要等待另一次使用才能花时间。

You could handle this problems with come solutions, but all then probably will be something similar to unite the programs. 您可以使用随附的解决方案来解决此问题,但所有这些可能都将类似于将程序组合在一起。

basic ideia: Mix both the programs in just one. 基本思想:将两个程序混为一谈 Singular process/thread to rule all the reads. 单个进程/线程统治所有读取。

improved ideia: Divide your code into three process. 改进的想法:将您的代码分为三个过程。 Two of then will just read/write a array and use that value as it was from GPIO. 然后,其中两个将只读/写一个数组,并使用该值,就像从GPIO一样。 One of that process will be your main process and will a SharedArray to handle the GPIO data. 其中一个过程将是您的主要过程,并将使用一个SharedArray处理GPIO数据。 Keeping in mind that read from RAM is much better/faster than read from GPIO, so just use the Array to update the status from IO. 请记住,从RAM读取比从GPIO读取要好/快得多,因此只需使用阵列从IO更新状态即可。 You could use one array to set the new values and another to store the actual values. 您可以使用一个数组设置新值,并使用另一个数组存储实际值。

Some time ago i used some Sockets to broadcast some IO data over some threads and could get a updated data (picture from camera) and share it with the threads with a gap of 100ms, just avoiding the IO gap. 前一段时间,我使用一些套接字在一些线程上广播一些IO数据,并且可以获取更新的数据(来自摄像机的图片)并与这些线程共享,间隔为100ms,只是避免了IO间隔。 So you could get a similar result using the SocketServer lib with some workers to spread the info. 因此,使用带有某些工作程序SocketServer lib可以得到类似的结果,以传播信息。

Instead of running the programs at the same time, try running them one after another. 与其同时运行程序,不如尝试一个接一个地运行它们。 Obviously the Raspberry Pi has hardware limitations due to it's size and cost. 显然,由于Raspberry Pi的尺寸和成本,它具有硬件限制。 What I would suggest is to have only one program to run at a time. 我建议一次只运行一个程序。 You could put them in a basic loop where you run program 1, then run program 2, then go back to the beginning of the loop. 您可以将它们置于一个基本循环中,在该循环中,您可以运行程序1,然后运行程序2,然后回到循环的开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM