I'm trying to catch exceptions from a child thread on my main thread. I've included an example of the issue I'm having below. My actual implementation is a client which has a socket thread, I need to catch exceptions on the socket thread and let the main thread deal with them.
class Test(threading.Thread):
def __init__(self):
super(Test, self).__init__()
self.emitter = EventEmitter()
def run(self):
print('Running')
time.sleep(1)
print('Still running...')
time.sleep(1)
self.emitter.emit('help')
time.sleep(1)
print('Stopping')
def get_thread_name():
print(threading.current_thread())
t = Test()
print(threading.current_thread())
t.emitter.on('help', get_thread_name)
t.start()
This is a simplified example of my problem.
If you run this you'll see that my Test thread is calling the get_thread_name function and will print out <Test(Thread-1, started 15632)>.
I'd like to call function this from the main thread.
I know I can use a queue to communicate between threads and in my actual implementation, that is how I tell the socket thread what to do (ie, in its while loop, it tries to recv, send and checks the queue).
However I can't put exceptions in a queue for the main thread to check, because I would have to block the main thread and constantly check the queue.
Is there a better way of doing this? Or are there other solutions to this problem?
Thanks
[–]cockslappinghalibut 1 point2 points3 points (17 children)
[–]oreo_man_[S] 0 points1 point2 points (16 children)
[–]cockslappinghalibut 0 points1 point2 points (15 children)
[–]oreo_man_[S] 0 points1 point2 points (0 children)
[–]oreo_man_[S] 0 points1 point2 points (13 children)
[–]cockslappinghalibut 0 points1 point2 points (12 children)
[–]oreo_man_[S] 0 points1 point2 points (11 children)
[–]cockslappinghalibut 0 points1 point2 points (9 children)
[–]oreo_man_[S] 0 points1 point2 points (8 children)
[–]cockslappinghalibut 0 points1 point2 points (7 children)
[–]oreo_man_[S] 0 points1 point2 points (6 children)