Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x Python 3:';多处理';和';时间';模块不兼容?_Python 3.x_Time_Multiprocessing_Python Multiprocessing - Fatal编程技术网

Python 3.x Python 3:';多处理';和';时间';模块不兼容?

Python 3.x Python 3:';多处理';和';时间';模块不兼容?,python-3.x,time,multiprocessing,python-multiprocessing,Python 3.x,Time,Multiprocessing,Python Multiprocessing,我使用multiprocessing.Pool().imap_unordered(…)并行执行一些任务,并通过计算启动池任务前后time.time()的差值来测量所花费的时间 但是,它返回错误的结果!当我在程序运行时看挂钟时,它告诉我大约5秒的运行时间。但是程序本身只输出0.1秒的运行时间 我还有一个没有任何多处理的代码变体,它花费了双倍的时间,但是输出了正确的运行时间 这是我的密码: if __name__ == "__main__": n = int(input("How many

我使用
multiprocessing.Pool().imap_unordered(…)
并行执行一些任务,并通过计算启动池任务前后
time.time()
的差值来测量所花费的时间

但是,它返回错误的结果!当我在程序运行时看挂钟时,它告诉我大约5秒的运行时间。但是程序本身只输出0.1秒的运行时间

我还有一个没有任何多处理的代码变体,它花费了双倍的时间,但是输出了正确的运行时间

这是我的密码:

if __name__ == "__main__":

    n = int(input("How many grids to create? "))
    use_multiprocessing = None
    while use_multiprocessing is None:
        answer = input("Use multiprocessing to speed things up? (Y/n) ").strip().lower()
        if len(answer) == 1 and answer in "yn":
            use_multiprocessing = True if answer == "y" else False
    t0 = time.time()

    if use_multiprocessing:
        processes = cpu_count()
        worker_pool = Pool(processes)

        print("Creating {} sudokus using {} processes. Please wait...".format(n, processes))
        sudokus = worker_pool.imap_unordered(create_sudoku, range(n), n // processes + 1)

    else:
        progress_bar, progress_bar_length = 0, 10
        sudokus = []

        print("Creating {} sudokus".format(n), end="", flush=True)
        for i in range(n):
            p = int((i / n) * progress_bar_length)
            if p > progress_bar:
                print("." * (p-progress_bar), end="", flush=True)
                progress_bar = p
            new_sudoku = create_sudoku()
            sudokus.append(new_sudoku)

    t = time.time() - t0
    l = len(list(sudokus))
    print("\nSuccessfully created {} grids in {:.6f}s (average {:.3f}ms per grid)!".format(
        l, t, 1000*t/l
    ))
这里是一个示例运行,实际运行大约需要5-6秒(当然,在输入要创建的网格数以及是否使用多处理之后):

多处理
time.time()
是否不兼容?我听说在这种情况下,
time.clock()
会出问题,但我认为
time.time()
应该是安全的。或者还有其他问题吗?

我已经解决了

Pool.imap_unordered(…)
返回一个生成器,但不返回列表。这意味着,当方法完成时,它的元素还没有被创建,而是在我访问它们时才被创建

我在
l=len(list(sudokus))
行中这样做,我将生成器转换为一个列表以获得长度。完成时间在这之前测量了一行,所以它正确地报告了初始化生成器所用的时间。这不是我想要的,所以交换这两行可以得到正确的时间

我知道我可能不会将生成器转换为列表,只是为了找出长度,然后再次丢弃列表。如果我想要一个生成器,我必须依赖保存的请求长度,或者我必须使用
Pool.map(…)
来生成列表和块,直到它准备好。

我找到了它

Pool.imap_unordered(…)
返回一个生成器,但不返回列表。这意味着,当方法完成时,它的元素还没有被创建,而是在我访问它们时才被创建

我在
l=len(list(sudokus))
行中这样做,我将生成器转换为一个列表以获得长度。完成时间在这之前测量了一行,所以它正确地报告了初始化生成器所用的时间。这不是我想要的,所以交换这两行可以得到正确的时间

我知道我可能不会将生成器转换为列表,只是为了找出长度,然后再次丢弃列表。如果我想要一个生成器,我必须依赖保存的请求长度,或者我必须使用
Pool.map(…)
来生成列表和块,直到它准备好为止

How many grids to create? 100000
Use multiprocessing to speed things up? (Y/n) y
Creating 100000 sudokus using 4 processes. Please wait...

Successfully created 100000 grids in 0.122141s (average 0.001ms per grid)!

Process finished with exit code 0