小编Mat*_*son的帖子

Python多处理Pool.map正在调用aquire?

我有一个640x480图像的numpy.array,每个图像长630像.因此总阵列为630x480x640.我想生成一个平均图像,并计算所有630个图像中每个像素的标准偏差.

这很容易实现

avg_image = numpy.mean(img_array, axis=0)
std_image = numpy.std(img_array, axis=0)
Run Code Online (Sandbox Code Playgroud)

但是,由于我运行50个左右这样的数组,并且有一个8核/ 16线程工作站,我想我会变得贪婪并使用multiprocessing.Pool并行化.

所以我做了以下事情:

def chunk_avg_map(chunk):
    #do the processing
    sig_avg = numpy.mean(chunk, axis=0)
    sig_std = numpy.std(chunk, axis=0)
    return([sig_avg, sig_std])

def chunk_avg(img_data):

    #take each row of the image
    chunks = [img_data[:,i,:] for i in range(len(img_data[0]))]

    pool = multiprocessing.Pool()
    result = pool.map(chunk_avg_map, chunks)
    pool.close()
    pool.join()
    return result
Run Code Online (Sandbox Code Playgroud)

但是,我只看到了一个小的加速.通过在chunk_avg_map中放置print语句,我能够确定一次只启动一个或两个进程,而不是16个(正如我所期望的那样).

然后我通过iPython中的cProfile运行我的代码:

%prun current_image_anal.main()
Run Code Online (Sandbox Code Playgroud)

结果表明,到目前为止,大部分时间花费在获取电话上:

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
     1527  309.755    0.203  309.755    0.203 {built-in method acquire}
Run Code Online (Sandbox Code Playgroud)

我理解与锁定有关,但我不明白为什么我的代码会这样做.有没有人有任何想法?

[编辑]根据要求,这是一个可运行的脚本,演示了这个问题.您可以通过任何您喜欢的方式对其进行分析,但是当我这样做时,我发现狮子分享的时间用于获取,而不是按照我的预期调整或调整.

#!/usr/bin/python
import numpy
import multiprocessing …
Run Code Online (Sandbox Code Playgroud)

python profiling multiprocessing

12
推荐指数
1
解决办法
1694
查看次数

在将Python 2代码移植到Python 3时处理ctypes和ASCII字符串

昨晚我厌倦了并开始将PyVISA移植到Python 3(这里的进展:https://github.com/thevorpalblade/pyvisa).

只要我将设备地址(嗯,任何字符串确实)作为ASCII字符串而不是默认的unicode字符串
传递(例如,HP = vida.instrument(b"GPIB:),我已经达到了一切正常的程度.:16")工作,而HP = vida.instrument("GPIB :: 16")不起作用,引发ValueError.

理想情况下,最终用户不必关心字符串编码.关于我应该如何处理的任何建议?ctypes类型定义中的某些东西也许?

就目前而言,相关的ctypes类型定义是:

ViString = _ctypes.c_char_p
Run Code Online (Sandbox Code Playgroud)

python porting ctypes python-3.x visa

3
推荐指数
1
解决办法
1392
查看次数

标签 统计

python ×2

ctypes ×1

multiprocessing ×1

porting ×1

profiling ×1

python-3.x ×1

visa ×1