【问题标题】:Python multithread not efficientPython多线程效率不高
【发布时间】:2021-04-25 17:31:45
【问题描述】:

我尝试比较许多处理器和许多编程语言的速度,但我在 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)

1 个线程和 1 个 resolve()=> 6 秒,2 个线程和 2 个 resolve() => 12 秒

resolve() 中的 print() 你在这里看不到,我读到了:

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

似乎有什么我不明白的:/ 谢谢!

【问题讨论】:

标签: python multithreading python-multiprocessing python-multithreading


【解决方案1】:

realpython.com/python-gil

有趣的谢谢!

【讨论】:

    猜你喜欢
    • 2011-06-20
    • 1970-01-01
    • 1970-01-01
    • 2011-05-24
    • 1970-01-01
    • 1970-01-01
    • 2016-01-19
    • 1970-01-01
    • 2021-11-08
    相关资源
    最近更新 更多