documentation for run_in_thread with exception_handler seems to be misleading · Issue #3464 · redis/redis-py (original) (raw)
Version: 5.0.5
but really the current online documentation https://redis-py.readthedocs.io/en/stable/advanced_features.html#publish-subscribe
Platform: Python 3.12 fedora40
Description:
The documentation for run_in_thread with exception_handler is misleading. Sample code you provide (with annotations)
p.subscribe(**{'my-channel': my_handler})
def exception_handler(ex, pubsub, thread):
print(ex)
thread.stop()
thread.join(timeout=1.0) # this raises RuntimeError("cannot join current thread")
pubsub.close() # this never gets called
thread = p.run_in_thread(exception_handler=exception_handler)
The handler runs in the context of the thread, and the thread argument is "self".
Calling join() on itself, the handler will raise RuntimeError("cannot join current thread"), and exit "uncleanly" (without closing pubsub). so it works, but it seems a bit misleading.
These might be better examples for the documentation:
def exception_handler(ex, pubsub, thread):
print(ex)
thread.stop()
or
def exception_handler(ex, pubsub, thread):
print(ex)
pubsub.close()
raise ex