Cameron
Cameron

Reputation: 85

Python Multi-Threading Basics

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?

Upvotes: 0

Views: 100

Answers (1)

rdas
rdas

Reputation: 21275

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

Upvotes: 2

Related Questions