0

I am having trouble understanding how to get simple multi-threading to work in python. Here is a simple script I have written in python that should simultaneously write to two different files:

from threading import Thread
import time

def function(file):
    with open(file, 'w') as f:
        i = 0
        while i < 10:
            print(file + ' printing ' + str(i))
            f.write(str(i) + '\n')
            time.sleep(0.4)
            i += 1

if __name__ == '__main__':
    thr1 = Thread(target=function('thr1.txt'))
    thr2 = Thread(target=function('thr2.txt'))

    thr1.start()
    thr2.start()

The output of this code being run suggests that these functions are not being executed in parallel but instead one after the other:

thr1.txt printing 0
thr1.txt printing 1
thr1.txt printing 2
thr1.txt printing 3
thr1.txt printing 4
thr1.txt printing 5
thr1.txt printing 6
thr1.txt printing 7
thr1.txt printing 8
thr1.txt printing 9
thr2.txt printing 0
thr2.txt printing 1
thr2.txt printing 2
thr2.txt printing 3
thr2.txt printing 4
thr2.txt printing 5
thr2.txt printing 6
thr2.txt printing 7
thr2.txt printing 8
thr2.txt printing 9

Process finished with exit code 0

Have I misunderstood the basics of multithreading functions in python because from the resources I have looked at this appears to be the way it is done?

1 Answer 1

2

Here:

    thr1 = Thread(target=function('thr1.txt'))
    thr2 = Thread(target=function('thr2.txt'))

Calls to function are evaluated eagerly. Basically you are passing the result of calling function as target to Thread. So your functions are executed even before the threads are created.

What you need to do is pass the name of the function to invoke & its arguments - without invoking the function.

thr1 = Thread(target=function, args=('thr1.txt',))
thr2 = Thread(target=function, args=('thr2.txt',))

And this results in interleaved output as expected:

thr1.txt printing 0
thr2.txt printing 0
thr2.txt printing 1
thr1.txt printing 1
thr2.txt printing 2thr1.txt printing 2

thr2.txt printing 3thr1.txt printing 3

thr2.txt printing 4
thr1.txt printing 4
thr1.txt printing 5
thr2.txt printing 5
thr2.txt printing 6
thr1.txt printing 6
thr2.txt printing 7
thr1.txt printing 7
thr1.txt printing 8thr2.txt printing 8

thr2.txt printing 9thr1.txt printing 9
Sign up to request clarification or add additional context in comments.

2 Comments

+1 Was on my way to writing something similar. I think it's also worth noting, @Cameron, that Python does not actually execute the threads in true parallel fashion do the GIL. See here for reference.
@Felipe Actually, in this example they pretty much do execute in parallel if the OS allow it and schedule the threads at different cores. Both threads are IO bound (as all they do are pausing and writing to the screen). Now, this is a somewhat silly example of course and in general what you say still holds. But it is still good to note, that for IO bound programs, threads can be "truly" parallel.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.