Search code examples
pythonmultithreadingpython-2.7multicore

Python multithreading for separate mutually exclusive tasks


Say you have 4 numbers, and you need to print the multiplication table of all the numbers from 1 to 100 using multithreading in Python. The code that I came up with is

from threading import Thread

def multable(r, number):
    for i in range(1,101):
        mul = number*i
        print "\n%d x %d = %d" %(number, i, mul)


def Main():
    t1 = Thread(target = multable, args = (1, 2))
    t2 = Thread(target = multable, args = (1, 3))
    t3 = Thread(target = multable, args = (1, 4))
    t4 = Thread(target = multable, args = (1, 4))
    t1.start()
    t2.start()
    t3.start()
    t4.start()

if __name__ == '__main__':
    Main()

Partial Output -

4 x 60 = 240

4 x 61 = 244

4 x 62 = 248

3 x 48 = 144

2 x 78 = 156
4 x 63 = 252
3 x 49 = 147



3 x 50 = 150

2 x 79 = 158
3 x 51 = 153


4 x 64 = 256

My questions regarding this are -

  • Why is there uneven spacing, as I think there should only one line gap between two outputs, by the \n in the print statement?
  • Why can't I just pass only one variable as parameter in the thread, the error I get in doing so asks me to give an iterative, so I gave an useless value?
  • In a quad-core CPU, how can I edit this program to make use of all four CPUs, each for one number/thread. disregarding the order of output?

Solution

  • Why is there uneven spacing, as I think there should only one line gap between two outputs, by the \n in the print statement?

    I think you get the spacing issue because python flushes the standard output buffer on each new line. Since you are doing it in multiple threads there is no guarantee of ordering which leads the data written in each flush to be written in a random order.

    Why can't I just pass only one variable as parameter in the thread, the error I get in doing so asks me to give an iterative, so I gave an useless value?

    If you place single a value in () such as (5) it really just means 5. You need to add a comma for python to think it is a tuple (5,). Or you can use lists [5].

    In a quad-core CPU, how can I edit this program to make use of all four CPUs, each for one number/thread. disregarding the order of output?

    This should happen automatically. It will be up to the operating system to schedule it. If it sees 4 separate threads it should place one on each core (depending on the task scheduler.) You will likely not be able to see all 4 cores at 100% in your task manager for two reasons. First is that the python interpreter does not really support multi-threading. Every python statement is locked so only one statement can execute at a time. You can get around this if you are creating C extensions for python, but all calls to the python interpreter need to be locked. Secondly even if you wrote the same thing in C I doubt you would see this burning up all cores in your task manager. It is a very light and IO heavy process which does not use very much CPU.