I am trying to run the below code expecting it to run asynchronously.
import threading
import time
def mock_app(widgets=None, filters=None):
print('Mock app called')
task = threading.Thread(target=mock_app_handler(), args=())
task.start()
print('Thread spawned')
return "success"
def mock_app_handler():
# do something
print('doing something')
time.sleep(2)
print('done something')
print(mock_app())
But, the code is executing synchronously. I am getting the below result.
Mock app called
doing something
done something
Thread spawned
success
Why is this happening? Am I missing something?
UPDATE: I tried setting task.daemon = True. That didn't work either.
task = threading.Thread(target=mock_app_handler)# don't call the function there, instead pass the function object. Also you don't need theargsthere, since the function doesn't take any arguments