我有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) 作为练习,我想构造一个包含std::array<unsigned char, N>对象(其中N不同)的向量。
我的尝试是构造一个基类, GenericArraya 将从该基类MyArray<N>派生,这样容器实际上将是:std::vector<GenericArray*>。但是,由于实际的数组变量必须驻留在派生类中,因此我没有找到从其本身使用这些数据的方法std:vector<GenericArray*>。
这是我的完整尝试,显然会产生:error: \xe2\x80\x98class GenericArray\xe2\x80\x99 has no member named \xe2\x80\x98data\xe2\x80\x99
#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) 我发现了一种我认为可以更优雅地表达的模式:
我有两个函数f1,f2 :: Int -> Int(它们的实现不相关),以及一个process :: Int -> Int执行以下操作的函数:
f1 x产生的结果x1与 不同x,则重复该过程x1f2 x产生的结果x2与 不同x,则重复该过程x2x我的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)