나는 Raspberry Pi 2에서 rospy 프로젝트를 작업 중입니다. pythton 스크립트는 메시지가 수신 될 때마다 스레드를 시작하고 다른 메시지가 나타날 때까지 반복해야합니다. 받았습니다. 하지만이 스레드는 하드웨어에 액세스해야하기 때문에 새 메시지를받을 때마다 이전 스레드가 중지되어야합니다. 나만의 잠금 시스템을 구축하지만 메시지가 넘치면 실패합니다. 지금은 세마포어를 조사하고 있지만 어떻게 작동하는지 또는 다른 것을 필요로하지만 세마포어와 비슷합니다."무한 루프"에서 벗어나는 세마포어와 같은 객체가 필요합니다.
현재 내 코드는 다음과 같습니다
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