小编dda*_*lla的帖子

OSX和Linux之间的性能差异,用于使用Python多处理进行通信

我一直在尝试更多地了解Python的multiprocessing模块,并评估不同的流程之间的通信技术.我写的比较的性能的基准Pipe,QueueArray(都来自multiprocessing)用于传递numpy进程之间的阵列.完整的基准可以在这里找到.以下是测试的片段Queue:

def process_with_queue(input_queue, output_queue):
    source = input_queue.get()
    dest = source**2
    output_queue.put(dest)


def test_with_queue(size):

    source = np.random.random(size)

    input_queue = Queue()
    output_queue = Queue()

    p = Process(target=process_with_queue, args=(input_queue, output_queue))
    start = timer()
    p.start()
    input_queue.put(source)
    result = output_queue.get()
    end = timer()

    np.testing.assert_allclose(source**2, result)

    return end - start
Run Code Online (Sandbox Code Playgroud)

我在我的Linux笔记本电脑上运行了这个测试,并获得了数组大小为1000000的以下结果:

Using mp.Array: time for 20 iters: total=2.4869s, avg=0.12435s
Using mp.Queue: time for 20 iters: total=0.6583s, avg=0.032915s
Using mp.Pipe:  time …
Run Code Online (Sandbox Code Playgroud)

python linux macos communication multiprocessing

26
推荐指数
1
解决办法
1009
查看次数

pytest autouse 夹具导致测试失败

pytest允许创建自动应用于测试套件中每个测试的夹具(通过autouse关键字参数)。这对于实现影响每个测试用例的设置和拆卸操作很有用。更多细节可以在pytest 文档中找到。

理论上,相同的基础设施对于验证每次测试运行后预期存在的后置条件也非常有用。例如,可能每次测试运行时都会创建一个日志文件,我想确保在测试结束时它存在。

不要拘泥于细节,但我希望你了解基本概念。关键是将这些代码添加到每个测试函数中会很乏味和重复,尤其是当autouse夹具已经提供了将此操作应用于每个测试的基础架构时。此外,fixtures 可以打包成插件,所以我的检查可以被其他包使用。

问题是似乎不可能从夹具中导致测试失败。考虑以下示例:

@pytest.fixture(autouse=True)
def check_log_file():
    # Yielding here runs the test itself
    yield

    # Now check whether the log file exists (as expected)
    if not log_file_exists():
        pytest.fail("Log file could not be found")
Run Code Online (Sandbox Code Playgroud)

在日志文件不存在的情况下,我不会得到测试失败。相反,我收到了 pytest 错误。如果我的测试套件中有 10 个测试,并且它们都通过了,但是其中 5 个缺少日志文件,我将得到 10 个通过和 5 个错误。我的目标是获得 5 次传球和 5 次失败。

所以第一个问题是:这可能吗?我只是错过了什么吗?这个答案向我表明这可能是不可能的。如果是这样,那么第二个问题是:还有其他方法吗?如果这个问题的答案也是“不”:为什么不呢?这是pytest基础设施的根本限制吗?如果没有,那么是否有计划支持这种功能?

unit-testing fixtures pytest

5
推荐指数
1
解决办法
2406
查看次数