0

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?

user9371654
  • 2,160
  • 16
  • 45
  • 78
  • You can pass the number of threads as your `range` i.e. `for i in range(your_var)`. For writing the file at the same time you can have a look at [this question](https://stackoverflow.com/q/11983938/4636715) if you mean writing to the same file in parallel. – vahdet Mar 01 '19 at 08:55
  • @vahdet Thanks. By writing to a file, I mean the target function performs operation in the input whi I sent in the args, then it writes the output to a file. Each input has its output written in a line in the same file. I am not sure how the multithreads will be writing the output to the same file? Is this will be managed by python? Or should I do any precautions. For example, multiple threads will write to the same line (each input should have its output in a single line). I want to be cautious not to cause data confusion. – user9371654 Mar 01 '19 at 09:05

1 Answers1

0

1) How can I specify the number of threads in the above code?

The argument to range function are your number of threads.

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?

Yes multi-threading in python can be really weird if you are not careful on managing your data structures that your threader function is using. So in your case in a naive implementation you can have multiple threads writing same line to the file if the writeoperation is not controlled. That said what you can probably do in your case is to have the lines from input file put in to a queue and then get the line in the threader function and write it to the output file.

Rohit
  • 3,659
  • 3
  • 35
  • 57