1# test raising and catching an exception within a thread 2# 3# MIT license; Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd 4 5import _thread 6 7 8def foo(): 9 raise ValueError 10 11 12def thread_entry(): 13 try: 14 foo() 15 except ValueError: 16 pass 17 with lock: 18 global n_finished 19 n_finished += 1 20 21 22lock = _thread.allocate_lock() 23n_thread = 4 24n_finished = 0 25 26# spawn threads 27for i in range(n_thread): 28 _thread.start_new_thread(thread_entry, ()) 29 30# busy wait for threads to finish 31while n_finished < n_thread: 32 pass 33print("done") 34