I am using the assertion in a multithreaded environment to help me catch early bugs. But it turns out that when assertion failed, python3.7 just terminates silently without printing the error message that I want to see. Thanks for helping in advance.
Here is the sample code:
from concurrent.futures import ThreadPoolExecutor
import threading
import random
from time import sleep
def task():
assert False, "no assertion output, why?"
print("Executing our Task")
sleep(5)
result = 0
i = 0
for i in range(10):
result = result + i
print("I: {}".format(result))
print("Task Executed {}".format(threading.current_thread()))
if __name__ == '__main__':
executor = ThreadPoolExecutor(max_workers=3)
task1 = executor.submit(task,1)
task2 = executor.submit(task,1)
#task(), directly calling and the assertion failure message will be printed out.
I ran it using the command python3.7 test.py
I think I used python assertion in a wrong way because I assume it to be the assert function in c++. If I change the assertion to throwing an exception, the above code does not print too. Am I supposed to catch the exception?
BTW, directly calling it, and the assertion failure message will be shown though.
taskfunction directly? - Henry Woodyassert False, ("no assertion output, why?")- Simbarashe Timothy Motsi