I am new to python multi threading programming. But to run my function in a reasonable time, I have to use it. In an online tutorial, I found this basic code:
import threading
def f(id):
print ("thread function:",id)
return
for i in range(3):
t = threading.Thread(target=f, args=(i,))
t.start()
The output I got is:
thread function: 0
thread function: 1
thread function: 2
In my actual program, I have parameters, which are a line to be read from a local file. I want to pass it to the target function. The target function performs some task, then writes results to a local file too.
My questions: 1) How can I specify the number of threads in the above code?
2) Is there any problem in making the target function writes results to a file? Can the multiple threads write at the same time? Are there any precautions to take to avoid mistakes?