[Python] Wake up a blocking redis call inside a signal handler in python

Discussão em 'Python' iniciado por Stack, Março 7, 2025.

  1. Stack

    Stack Membro Participativo

    I need to wake up a blocked pop (redis client) on a list thru a signal handling (in this case SIGUSR1)

    This is the sample code:

    import redis
    import signal

    r = redis.Redis()

    def handler(signal, frame):
    q = redis.Redis()
    q.rpush('test', 1)
    print 'sign arrived'

    signal.signal(signal.SIGUSR1, handler)

    print 'start'
    print r.blpop('asd')
    print 'end'


    the output of this code is

    start
    // here I send the signal with "$ kill -10 <pid>"
    sign arrived


    and stop

    other informations:

    • the result of the push inside the handler is 1 (it should mean that an element have actually been inserted in the list)
    • if I open a parallel redis-cli shell, no element is shown with "LRANGE" even after the signal handling
    • redis-cli command "CLIENT LIST" shows the blpop client

    What is wrong? thanks in advance

    update


    if I duplicate the push, works:

    def handler(signal, frame):
    q = redis.Redis()
    q.rpush('test', 1)
    q.rpush('test', 1)
    print 'sign arrived'


    And no other message seems last in the redis queue. It looks like the first push disappears...

    Continue reading...

Compartilhe esta Página