I'm running the following code:
from threading import Thread
from time import sleep
def file_write(file_input, num_lines):
sleep(10)
file = open("testfile.txt", "w")
for line in num_lines:
file.write("{}: {}".format(line, file_input))
file.close()
if __name__ == '__main__':
curr_thread = Thread(target=file_write, args=("Norah", range(5)))
curr_thread.daemon = False
curr_thread.start()
The expectation is the main thread will exit immediately, because I don't call join. But it doesn't. Do sleep calls block the main thread too?
EDIT: There is a similar question asked in this thread: time.sleep -- sleeps thread or process? but it's not the same.
I looked at the thread: it says that sleep doesn't cause child processes to block each other, but it doesn't say what happens to the main thread. When I ran the code from the accepted answer, the main thread did not exit immediately, as I thought it would.
UPDATE: Looks like multithreading won't solve my problem: the aim is to run a bunch of tasks in the background. I'm using the subprocess module instead now.