我在基于Intel i3的计算机上运行以下代码,该计算机具有4个虚拟核心(2个超线程/物理核心,64位)和安装的Ubuntu 14.04:
n = multiprocessing.cpu_count()
executor = ThreadPoolExecutor(n)
tuple_mapper = lambda i: (i, func(i))
results = dict(executor.map(tuple_mapper, range(10)))
Run Code Online (Sandbox Code Playgroud)
代码似乎没有以并行方式执行,因为CPU的使用率仅为25%.在利用率图表中,一次仅100%使用4个虚拟核心中的一个.使用的核心每10秒左右交替一次.
但是并行化在具有相同软件设置的服务器计算机上运行良好.我不知道核心的确切数量,也不知道确切的处理器类型,但我确信它有几个核心,利用率为100%,并且计算速度快(使用并行化后速度提高了10倍)一些实验用它).
我希望,并行化也可以在我的机器上运行,而不仅仅是在服务器上.
为什么不起作用?它与我的操作系统设置有关吗?我需要改变它们吗?
提前致谢!
更新: 有关背景信息,请参阅下面的正确答案.为了完整起见,我想提供一个解决问题的示例代码:
tuple_mapper = lambda i: (i, func(i))
n = multiprocessing.cpu_count()
with concurrent.futures.ProcessPoolExecutor(n) as executor:
results = dict(executor.map(tuple_mapper, range(10)))
Run Code Online (Sandbox Code Playgroud)
在重用之前,请注意您正在使用的所有函数都在模块的顶层定义,如下所述: Python多处理酸洗错误
是否可以通过蓝牙以编程方式从QardioCore检索原始ECG信号?我只有一个Android设备,而且由于Android Qardio应用程序无法用于QardioCore,我想知道是否有人已经尝试编写自己的应用程序以用于Android,如果它有效?
我正在使用Sphinx(make HTML)从其函数的reStructuredText文档字符串自动生成Python 3模块的HTML文档.到目前为止,生成的HTML文档看起来很好,但函数签名的参数类型(在源代码中作为PEP484类型提示给出)未正确显示.
例如,这是我的一个函数的Sphinx生成的HTML文档的一些示例输出:
static parse_from_file(filename: str) ? list
Parses stuff from a text file.
Parameters: filename – the filepath of a textfile to be parsed
Returns: list of parsed elements
Run Code Online (Sandbox Code Playgroud)
这就是我期望的样子:
static parse_from_file(filename)
Parses stuff from a text file.
Parameters: filename (str) – the filepath of a textfile to be parsed
Returns: list of parsed elements
Return type: list
Run Code Online (Sandbox Code Playgroud)
这就是Python代码的实际外观:
def parse_from_file(filename: str) -> list:
"""Parses stuff from a text file.
:param filename: the filepath of a …Run Code Online (Sandbox Code Playgroud)