小编Ale*_*inu的帖子

multiprocessing.Pool 中偶尔出现死锁

我有N独立的任务,它们以一定multiprocessing.Pool大小os.cpu_count()(在我的例子中为 8)执行maxtasksperchild=1(即为每个新任务创建一个新的工作进程)。

主脚本可以简化为:

import subprocess as sp
import multiprocessing as mp

def do_work(task: dict) -> dict:
    res = {}
    
    # ... work ...
   
    for i in range(5):
        out = sp.run(cmd, stdout=sp.PIPE, stderr=sp.PIPE, check=False, timeout=60)
        res[i] = out.stdout.decode('utf-8')

    # ... some more work ...

    return res


if __name__ == '__main__':
    tasks = load_tasks_from_file(...) # list of dicts

    logger = mp.get_logger()
    results = []

    with mp.Pool(processes=os.cpu_count(), maxtasksperchild=1) as pool:
        for i, res in enumerate(pool.imap_unordered(do_work, …
Run Code Online (Sandbox Code Playgroud)

python deadlock multiprocessing

7
推荐指数
1
解决办法
6107
查看次数

不同大小的 std::array 的 std::vector

作为练习,我想构造一个包含std::array<unsigned char, N>对象(其中N不同)的向量。

\n

我的尝试是构造一个基类, GenericArraya 将从该基类MyArray<N>派生,这样容器实际上将是:std::vector<GenericArray*>。但是,由于实际的数组变量必须驻留在派生类中,因此我没有找到从其本身使用这些数据的方法std:vector<GenericArray*>

\n

这是我的完整尝试,显然会产生:error: \xe2\x80\x98class GenericArray\xe2\x80\x99 has no member named \xe2\x80\x98data\xe2\x80\x99

\n
#include <array>\n#include <cassert>\n#include <iostream>\n#include <vector>\n\ntemplate<std::size_t N>\nusing arr_t = std::array<unsigned char, N>;\n\nclass GenericArray\n{\npublic:\n    ~GenericArray() = default;\n};\n\ntemplate<std::size_t N>\nclass MyArray : public GenericArray\n{\npublic:\n    arr_t<N> data;\n\n    MyArray(const arr_t<N>& data)\n    {\n        this->data = data;\n    }\n};\n\nint main(void)\n{\n    std::vector<GenericArray*> vs;\n\n    vs.emplace_back(new MyArray<2>({ \'a\', \'b\' }));\n    vs.emplace_back(new MyArray<4>({ \'A\', \'B\', \'C\', \'D\' }));\n\n    assert(vs.size() == 2);\n\n    for (const auto& …
Run Code Online (Sandbox Code Playgroud)

c++ containers

1
推荐指数
2
解决办法
524
查看次数

在 Haskell 中更优雅地表达“case ... of”模式

我发现了一种我认为可以更优雅地表达的模式:

我有两个函数f1,f2 :: Int -> Int(它们的实现不相关),以及一个process :: Int -> Int执行以下操作的函数:

  • 如果f1 x产生的结果x1与 不同x,则重复该过程x1
  • 否则,如果f2 x产生的结果x2与 不同x,则重复该过程x2
  • 最后,停止进程并返回x

我的case ... of实现如下:

f1 :: Int -> Int
f1 = undefined

f2 :: Int -> Int
f2 = undefined

process :: Int -> Int
process x =
    case f1 x of
        x ->
            case f2 x of
                x -> x
                x' -> …
Run Code Online (Sandbox Code Playgroud)

haskell pattern-matching fixed-point-iteration

1
推荐指数
2
解决办法
1447
查看次数