我正在Raspberry Pi 2上进行一个rospy项目。 pyhton脚本需要在接收到消息时启动一个线程,并循环它直到另一个消息接收。 但是由于这些线程需要访问硬件,所以每次收到新消息时都应该停止旧线程。 我建立了我自己的锁定系统,但是当充斥着消息时它失败了。 现在我正在研究信号量,但要么我不太了解它们的工作方式,要么我需要别的东西,但类似于信号量。需要跳出“无限循环”的信号量对象
目前我的代码看起来是这样的:
def some_thread():
# lock_int is 0 when noone uses the hardware, 1 when another thread uses it
global lock_int
# my way of signaling that I want to get the Hardware access
lock_int += 1
# wait until other thread releases Hardware
while(lock_int > 1):
wait()
# loop until some other thread requests hardware
while(lock_int < 2)
do_hardware_stuff()
# release hardware
lock_int -= 1
如果我想用旗语要做到这一点,我需要某种信号在do_hardware_stuff循环看到该请求。 事情是这样的:
def semaphore_thread():
# blocking request, waits until other thread releases
semaphore.acquire()
# loop until some other thread requests hardware
while(semaphore.got_request() != True)
do_hardware_stuff()
# release hardware
semaphore.release()
有没有一种方法,我可以用旗语或类似的这样的对象?
感谢, ChTe
但是,这不会循环do_harware_stuff()部分。 主要问题是在循环中获取某种通知以跳出并释放硬件。 – ChTe