5

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.

5
  • 2
    You may want to check this answer out: stackoverflow.com/a/1294402/58129 Commented Oct 27, 2013 at 3:16
  • 7
    Use multiprocessing instead of threading to get around the GIL limitation. Commented Oct 27, 2013 at 3:20
  • What Blender said. See this question for more details / examples -- stackoverflow.com/questions/17424569/… Commented Oct 27, 2013 at 3:22
  • 1
    Also, this answer outlines pros & cons of threads vs processes: stackoverflow.com/questions/3044580/… Commented Oct 27, 2013 at 3:24
  • I understand processes run in an separate memory space, but is there a way for processes to exchange data with the main thread of execution? Also, are these processes like the ones seen in Windows Task Manager? (sorry if that's a dumb question) Commented Oct 27, 2013 at 4:03

1 Answer 1

8

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks Rho! I'd upvote your comment but I lack the rep. Why is it that CPU-bound tasks can't run concurrently? Thinking of gaming, aren't most tasks CPU-bound? (like simulating something offscreen)
You can have concurrency in python. For an extended explanation see here: jessenoller.com/blog/2009/02/01/… For example, think having an (event) loop that performs a bit of a task in the first loop and a bit of another task in the next loop and so on. And take a look at this minecraft clone github.com/fogleman/Minecraft/blob/master/main.py#L409
Great info; I'll be studying it all in depth. Without the GIL, would my code run concurrently as is? Can other languages run CPU-bound threads in parallel?
The GIL thing is more about the implementation. The default implementation of python is CPython, there have been attempts to have a GIL-free python implementation but were unsuccessful. I can't say if all but many language have threads with true parallelism (i.e. c, java, etc). As a side note, event-driven concurrency is very successful for networking software.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.