1

I have two threads which are calling a function and that function is creating one more thread.

def run(urls):
    while len(urls) != 0:
        t3 = threading.Thread(target=scrap_matches, args=(urls[-1],))
        try:
            print("URL left", threading.currentThread().getName(), len(urls))
            t3.start()
            del urls[-1]
        except:
            print("Got Exception")


part_1, part_2 = parse(urls)

print(part_1)
print(part_2)

t1 = threading.Thread(target=run, args=(part_1,))
t2 = threading.Thread(target=run, args=(part_2,))

t1.start()
t2.start()

t1.join()
t2.join()

part_1 and part_2 are lists. When I got exception in function scrap_matches then instead of catching exception, thread breaks. I have searched a lot and I find few solutions but those are out of scope of my knowledge.

Abdul Haseeb
  • 75
  • 1
  • 8
  • 1
    Does this answer your question? [Catch a thread's exception in the caller thread in Python](https://stackoverflow.com/questions/2829329/catch-a-threads-exception-in-the-caller-thread-in-python) – Anmol Singh Jaggi Sep 19 '20 at 19:28

1 Answers1

0
from concurrent.futures import ThreadPoolExecutor:



threads = []

with ThreadPoolExecutor() as executer:

    t1 = executer.submit(run, part_1)
    t2 = executer.submit(rn, part_2)
    
    threads.append(t1)
    threads.append(t2)


    for thread in threads:
        try:
            thread.result()
        except ValueError as err:
            print(err)