Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python在do nothing循环中有多忙?_Python_Loops_Raspberry Pi - Fatal编程技术网

python在do nothing循环中有多忙?

python在do nothing循环中有多忙?,python,loops,raspberry-pi,Python,Loops,Raspberry Pi,上下文是raspberry pi,是指您对一件事情执行操作(例如发送信号)并等待某件事情发生(例如,在世界上最广泛的意义上,在另一个“通道”上接收响应),即某件事情可能“看起来”像一个呼叫,但实际上并非如此 例如,想象一个反应游戏,在这个游戏中,付款人必须在led点亮后快速按下按钮 led灯亮着 系统等待按钮按下 按下按钮时,等待结束并发生某些事情(例如,计算增量时间) 我的第一次(也是正在工作的)尝试和我在工作中发现的例子如下: def button_not_still_pressed()

上下文是raspberry pi,是指您对一件事情执行操作(例如发送信号)并等待某件事情发生(例如,在世界上最广泛的意义上,在另一个“通道”上接收响应),即某件事情可能“看起来”像一个呼叫,但实际上并非如此

例如,想象一个反应游戏,在这个游戏中,付款人必须在led点亮后快速按下按钮

  • led灯亮着
  • 系统等待按钮按下
  • 按下按钮时,等待结束并发生某些事情(例如,计算增量时间)
我的第一次(也是正在工作的)尝试和我在工作中发现的例子如下:

def button_not_still_pressed():
    return True if "Button has not been still pressed", e.g. voltage is low
    # this function does the check (e.g. reads a pin or performs any other test
    # depending on the application

while button_not_still_pressed():
    pass # Please note: non tome.sleep() here

do_whatever_when_button_is_pressed()
实际上,这似乎是可行的

我关注的是:

  • 不确定/长循环是否会导致系统挂起
  • 一般来说,python在上面的while循环中做什么
  • 特别是,对于这些设备,执行发送信号并等待的任务的最佳方式是什么
不确定/长循环是否会导致系统挂起

在更嵌入式的系统中,是的。在Linux中,可能不会,因为它没有做任何重要的事情

一般来说,python在上面的while循环中做什么

在占用CPU的紧密循环中运行其解释器

特别是,对于这些设备,执行发送信号并等待的任务的最佳方式是什么

如果您的按钮使用Raspberry Pi的GPIO,为避免出现死结:

GPIO.wait_for_edge(channel, GPIO.RISING)

API通常具有等待或通知机制。在这种情况下,python将转换到底层C库,该库在操作或超时发生之前执行线程级挂起。在此期间,CPU可以自由处理其他线程。在您的情况下,当轮询时,python试图消耗整个CPU。无论如何,操作系统可能会将它切换到其他任务,但您可能会认为这样的紧密循环会影响系统性能。