Search code examples
pythonmultithreadingpython-multiprocessingpython-multithreading

Python multithread not efficient


I try to compare many processors and many programming language speed and I have a problem with multithreading in python :

    def main(argv):
    start_time = time.time()

    case_number = 2
    nb_thread = 2
    t = [0] * nb_thread
    for i in range(0, nb_thread):
        t[i] = Thread(target=resolve, args=("TREAD " + str(i),)) // resolve is a mathematic problem (about 6 seconds to resolve it)
        t[i].start()

    t[0].join()
    t[1].join()
    print("--- %s seconds ---" % (time.time() - start_time))


if __name__ == '__main__':
    main(sys.argv)

With 1 thread and 1 resolve()=> 6 seconds and with 2 threads and 2 resolve() => 12 seconds

With print() in resolve() you don't see here, I read :

D:\Dev\python\Sudoku>python main.py txt.txt
TREAD 0   <<BEGUIN>>
TREAD 1   <<BEGUIN>>
TREAD 0   <<END>>
TREAD 1   <<END>>
--- 12.335727453231812 seconds ---

It seems there is something I don't understand :/ Thank you !


Solution

  • realpython.com/python-gil

    Interesting thank you !