多处理时间戳需要大量时间

时间:2018-11-21 06:41:00

标签: python multiprocessing

我创建了一个程序,可以生成进程并运行提供的算法。当我在不进行多处理的情况下运行该算法时,它需要1.7秒,但是当我生成两个运行同一算法的进程时,则需要18秒。

一个没有生成的进程的探查器结果: 68050 0.084 0.000 0.084 0.000 {method 'timestamp' of 'datetime.datetime' objects}

为其中一个生成的进程生成两个进程: 34025 15.947 0.000 15.947 0.000 {method 'timestamp' of 'datetime.datetime' objects}

为什么date.timestamp需要15.9秒?这是探查器错误吗?

生成:

# Start processes
for index, simulation in enumerate(self.simulations):
    proc = Process(target=simulation_runner, args=(simulation, queues[index]))
    jobs.append(proc)
    proc.start()

时间戳:

timestamp = math.floor(date.timestamp())

我还编写了执行相同操作的程序:

import time
from multiprocessing import Pool
from datetime import datetime


def worker(file_name):
    with open(file_name, 'r+') as file:
        for line in file:
            date = datetime.now()
            date.timestamp()


if __name__ == "__main__":
    start = time.time()
    args = ['FLT-TEST', 'FLT-TEST']
    pool = Pool()
    pool.map(worker, args)
    # worker('FLT-TEST')
    print(f'Executing in {time.time() - start} s.')

在不生成的情况下会打印Executing in 0.16368794441223145 s. 生成时会打印Executing in 66.31291604042053 s. FLT-TEST包含91849行

没有文件读取

import time
from multiprocessing import Pool
from datetime import datetime


def worker(num):
    print(f'Process {num}')
    for _ in range(95000):
        date = datetime.now()
        date.timestamp()


if __name__ == "__main__":
    start = time.time()
    args = [1, 2]
    pool = Pool()
    pool.map(worker, args)
    # worker(1)
    print(f'Executing in {time.time() - start} s.')

我得到相同的结果。

1 个答案:

答案 0 :(得分:1)

timestamp = math.floor(date.replace(tzinfo=timezone.utc).timestamp())

设置时区固定持续时间。