3

I have a raspberry pi which I have hooked up with a 4 button keypad. Using the signal stuff from blinker I hooked it up to run some methods.

#sender
while True:
    if buttonIsDown == True: signal.send()

#reciever
@signal.connect
def sayHI():
    print("1")
    time.sleep(10)
    print("2")

This works fine, however when I push the button for the second time (Within 10 seconds of the previous button press) it does not fire the method as the thread is paused in the time.sleep(10).

How can I get it to fire the method again while the it is still paused(possibly in another thread)

  • Could you point to the documentation (or your implementation) of the decorator `@signal.connect` as I only find doc for `@signal.connect_via`? – Adonis Aug 18 '17 at 08:53
  • @Adonis https://pythonhosted.org/blinker/ it is mentioned in the example in "Sending and Receiving Data Through Signals" –  Aug 18 '17 at 09:17

1 Answers1

4

It is an old question, but still it may be useful for someone else.

You can start a new thread every time the signal is emitted, in that way you will be able to catch all the events as soon as they happen. Remember that in your code, since you have a while True, the signal is never connected to the function, you should have defined them in the opposite order.

Here is a working example, based on your code:

import threading
from blinker import signal
from time import sleep

custom_signal = signal(name='custom')

@custom_signal.connect
def slot(sender):
    def say_hello():
        print("1")
        sleep(10)
        print("2")

    threading.Thread(target=say_hello).start()

while True:
    value = int(input('Press 1 to continue: '))
    if value == 1:
        custom_signal.send()
    else:
        break
Aquiles Carattino
  • 910
  • 1
  • 10
  • 23