Python parallel threads

18,612

You are executing the target function for the thread in the thread instance creation.

if __name__ == "__main__":
    t1 = threading.Thread(name="Hello1", target=testForThread1()) # <<-- here
    t1.start()

This is equivalent to:

if __name__ == "__main__":
    result = testForThread1() # == 'ok', this is the blocking execution
    t1 = threading.Thread(name="Hello1", target=result) 
    t1.start()

It's Thread.start()'s job to execute that function and store its result somewhere for you to reclaim. As you can see, the previous format was executing the blocking function in the main thread, preventing you from being able to parallelize (e.g. it would have to finish that function execution before getting to the line where it calls the second function).

The proper way to set the thread in a non-blocking fashion would be:

if __name__ == "__main__":
    t1 = threading.Thread(name="Hello1", target=testForThread1) # tell thread what the target function is
    # notice no function call braces for the function "testForThread1"
    t1.start() # tell the thread to execute the target function
Share:
18,612

Related videos on Youtube

Alex
Author by

Alex

Updated on September 29, 2022

Comments

  • Alex
    Alex over 1 year

    Here the code which download 3 files, and do something with it. But before starting Thread2 it waits until Thread1 will be finished. How make them run together? Specify some examples with commentary. Thanks

    import threading
    import urllib.request
    
    
    def testForThread1():
        print('[Thread1]::Started')
        resp = urllib.request.urlopen('http://192.168.85.16/SOME_FILE')
        data = resp.read()
        # Do something with it
        return 'ok'
    
    
    def testForThread2():
        print('[Thread2]::Started')
        resp = urllib.request.urlopen('http://192.168.85.10/SOME_FILE')
        data = resp.read()
        # Do something with it
        return 'ok'
    
    
    if __name__ == "__main__":
        t1 = threading.Thread(name="Hello1", target=testForThread1())
        t1.start()
        t2 = threading.Thread(name="Hello2", target=testForThread2())
        t2.start()
        print(threading.enumerate())
        t1.join()
        t2.join()
        exit(0)
    
    • Nisan.H
      Nisan.H
      You're executing the thread-target function in the thread assignment. Effectively, you are executing threading.Thread(name='Hello1', target='ok'). Try t1 = threading.Thread(name="Hello1", target=testForThread1) instead.