Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python threads don't run simultaneously

I'm brand new to multi-threaded processing, so please forgive me if I butcher terms or miss something obvious.

The code below doesn't offer any time advantage over different code that calls the same two functions one after the other.


import time
import threading

start_time = time.clock()

def fibonacci(nth): #can be ignored
    first = 0
    second = 1
    for i in range(nth):
        third = first + second
        first = second
        second = third
    print "Fibonacci number", i + 1, "is", len(str(first)), "digits long"

def collatz(collatz_max): #can be ignored
    for n in range(collatz_max):
        n = n + 1 #avoid entering 0
        solution = []
        solution.append(n)
        while n != 1:
            if n % 2 == 0:
                n = n / 2
            else:
                n = (n*3) + 1
            solution.append(n)
    print "Path for Collatz number", collatz_max, "is", solution

def scripts():
    thread_fibonacci = threading.Thread(target=fibonacci, args = (800000,))
    thread_collatz = threading.Thread(target=collatz, args = (400000,))

    thread_fibonacci.start()
    thread_collatz.start()

    return thread_fibonacci, thread_collatz

all_scripts = scripts()

#wait until both threads are finished
for script in all_scripts:
    script.join()

print time.clock() - start_time, "seconds"

What do I need to do to make the threads simultaneous? Does GIL mean concurrency can only be achieved through separate processes? If so, what is the point of multithreading?

Using Python 2.7.5 on Windows 8.1, quad-core processor. Any help would be appreciated.

like image 471
Jim V Avatar asked Oct 27 '13 03:10

Jim V


People also ask

How many threads can run simultaneously Python?

This means that in python only one thread will be executed at a time. By only allowing a single thread to be used every time we run a Python process, this ensures that only one thread can access a particular resource at a time and it also prevents the use of objects and bytecodes at once.

Can we run two threads simultaneously?

Within a process or program, we can run multiple threads concurrently to improve the performance. Threads, unlike heavyweight process, are lightweight and run inside a single process – they share the same address space, the resources allocated and the environment of that process.

Can Python threads run on multiple cores?

Threading Library. Above we alluded to the fact that Python on the CPython interpreter does not support true multi-core execution via multithreading. However, Python DOES have a Threading library.

Why Python is not good for multithreading?

Python doesn't support multi-threading because Python on the Cpython interpreter does not support true multi-core execution via multithreading. However, Python does have a threading library. The GIL does not prevent threading.


1 Answers

There are good answers regarding the GIL you can look at.

In short, if your tasks are CPU-bound (like the ones you posted), threads are not going to help you. Python threads are good for IO-bound tasks, like retrieving a web page.

like image 124
R. Max Avatar answered Sep 30 '22 09:09

R. Max