Thread in python-
<p>I want to use threads to get better performance in python.</p>

<p>My program need to return value from each function the thread does.</p>

<p>And I need to know when the thread is finished.</p>

<p>There are 3 ways I tried to execute this little program.</p>

<pre><code>import thread
import datetime
from threading import Thread
import threading
from multiprocessing.pool import ThreadPool


def func1(word):
    i=0
    while i&lt;100000:
        if 1&lt;2:
            i=i+1
    return "func1"


def func2():
    i=0
    while i&lt;100000:
        if 1&lt;2:
            i=i+1
    return "func2"

word="hiiii"

"""
#--------------------------------example1--------------------------------
pool = ThreadPool(processes=2)

print str(datetime.datetime.now().time())
async_result1 = pool.apply_async(func1, (word, ))

async_result2 = pool.apply_async(func2)

print async_result1.get()  
print  async_result2.get()  
print str(datetime.datetime.now().time())


print func1(word)
print func2()
print str(datetime.datetime.now().time())
#with threads-71000
#without threads- 40000
#--------------------------------example1--------------------------------
"""

"""
#--------------------------------example2--------------------------------

t1 = Thread(target=func1, args=(word,))
t2 = Thread(target=func2, args=())
print str(datetime.datetime.now().time())

t1.start()
t2.start()

t1.join()
t2.join()
print str(datetime.datetime.now().time())

func1(word)
func2()
print str(datetime.datetime.now().time())

#with threads-75000
#without threads-42000
#--------------------------------example2--------------------------------
"""

"""
#--------------------------------example3 without sending value--------------------------------
print str(datetime.datetime.now().time())

t1 = threading.Thread(name=func1,target=func1)
t2= threading.Thread(name=func2,target=func2)
t1.start()
t2.start()

t1.join()
t2.join()
print str(datetime.datetime.now().time())
func1()
func2()
print str(datetime.datetime.now().time())

#with threads- 73000
#without threads- 42000
#--------------------------------example3 without sending value------------- -------------------
   """
</code></pre>

<p>But, you can see that the better way to run is without threads! 
why?? what do I do wrong? How to use threads?</p>

<p>To append the other answer:</p>

<p>The reason the threaded version can actually be slower in a CPU-bound operation is (as mentioned) GIL, the Global Interpreter Lock. GIL works as a mutex, allowing only a single thread to access it at a time. So threading is only useful when waiting for IO-operations.</p>

<p>However, there is a way to actually execute code in multiple cores, bypassing the issue with GIL. The solution is to use <a href="https://docs.python.org/2/library/multiprocessing.html" rel="nofollow">multiprocessing</a> library.</p>

<p>So in your case:</p>

<pre><code>process1 = multiprocessing.Process(target=func1, args=(word,))
process1.start()
process2 = multiprocessing.Process(target=func2)
process2.start()

process.join()
process2.join()
</code></pre>

<p>The two processes should start in different cores, effectively making the program execute faster. For me it almost halved the execution time. Note that spawning more processes than you have cores will again make it run slower.</p>

python multithreading python-2.7 threadpool