Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python parallel threads

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)
like image 289
Alex Avatar asked Sep 03 '13 21:09

Alex


1 Answers

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
like image 175
Nisan.H Avatar answered Sep 18 '22 20:09

Nisan.H