为什么添加多个进程后任务变慢?

时间:2019-04-18 02:08:55

标签: python process

我想添加多个进程以加快程序运行速度,但发现添加多个进程后,程序执行时间变长了。我的代码如下。

'''before'''


if __name__ == '__main__':
    result = []
    start_time = int(time.time())
    for i in range(20000000):
    result.append(demo3(i, i + 1))
    end_time = int(time.time())

    print(result)
   print(end_time - start_time)


'''Add multiple processes  '''
def demo3(i, j):
    return int(i) * int(j)
if __name__ == '__main__':
    pool = multiprocessing.Pool(processes=10)
    result = []
    start_time = int(time.time())
    for i in range(20000000):
        result.append(pool.apply_async(demo3, args=(i, i + 1)).get())
    pool.close()
    pool.join()
    end_time = int(time.time())

    print(result)
    print(end_time - start_time)

0 个答案:

没有答案