python中导入numpy库_在并行Python中导入NumPy

1586010002-jmsa.png

everyone,I am new to PP but got stuck in a problem when importing NumPy with PP.

Basically what I tried to do was submitting a function to the ppserver which depends on NumPy. I have imported it at the very beginning of the code using (import NumPy as nu) but when I ran the code, it gave me the error that cannot find the shared object multiarray.so.

The situation is exactly the same here: parallel python forum

the code is attached as below: (I am running on python 2.7.2 + pp 1.6.0 + numpy 1.5.1)

import numpy as nu

import pylab as pl

import pp

job_server = pp.Server(secret="123456")

print "Starting pp with", job_server.get_ncpus(), "workers"

aa = GrRib()

job = job_server.submit(aa.plotwavefunc, (band,k),(nu,pl,signal))

result = job()

the error looks like :

An error has occured during the function import

Traceback (most recent call last):

File "/usr/lib/pymodules/python2.7/ppworker.py", line 86, in run

exec __fobj

File "", line 127, in

ImportError: Error importing numpy: you should not try to import numpy from

its source directory; please exit the numpy source tree, and relaunch

your python intepreter from there.

An error has occured during the function import

Traceback (most recent call last):

File "/usr/lib/pymodules/python2.7/ppworker.py", line 86, in run

exec __fobj

File "", line 1, in

File "/usr/share/pyshared/matplotlib/__init__.py", line 135, in

from matplotlib.rcsetup import (defaultParams,

File "/usr/share/pyshared/matplotlib/rcsetup.py", line 19, in

from matplotlib.colors import is_color_like

File "/usr/share/pyshared/matplotlib/colors.py", line 52, in

import numpy as np

File "/usr/share/pyshared/numpy/__init__.py", line 136, in

import add_newdocs

File "/usr/share/pyshared/numpy/add_newdocs.py", line 9, in

from numpy.lib import add_newdoc

File "/usr/share/pyshared/numpy/lib/__init__.py", line 4, in

from type_check import *

File "/usr/share/pyshared/numpy/lib/type_check.py", line 8, in

import numpy.core.numeric as _nx

File "/usr/share/pyshared/numpy/core/__init__.py", line 5, in

import multiarray

ImportError: No module named multiarray

Can anyone help me out? I understand it as I will have to change the directory PP is looking for pyshared objects.

解决方案

I could be wrong, but I believe the way you submit the job is wrong. As seen on the documentation (http://www.parallelpython.com/content/view/15/30/#QUICKSMP), the third argument (not including "self") are dependent functions. Also, the modules have to be strings. I'm assuming the "band" and "k" are dependent functions since I do not see their declarations:

job = job_server.submit(aa.plotwavefunc, depfuncs = (band,k), modules = ("numpy","pylab","signal"))

可以使用NumpyPython的多线程库进行图像处理的并行化。 首先,将图像加载到Numpy数组: ```python import numpy as np import cv2 img = cv2.imread('image.jpg') img = np.array(img) ``` 接下来,可以使用Numpy的矢量化函数对图像进行处理。例如,下面的代码使用Numpy的multiply函数将图像的每个像素的红色通道值乘以2: ```python def process_image(img): img[:, :, 0] = np.multiply(img[:, :, 0], 2) return img processed_img = process_image(img) ``` 要并行化这个处理过程,可以使用Python的多线程库。下面的代码使用4个线程并行化图像处理: ```python import threading def process_image_thread(img, start_row, end_row): img[start_row:end_row, :, 0] = np.multiply(img[start_row:end_row, :, 0], 2) threads = [] num_threads = 4 rows_per_thread = img.shape[0] // num_threads for i in range(num_threads): start_row = i * rows_per_thread end_row = start_row + rows_per_thread thread = threading.Thread(target=process_image_thread, args=(img, start_row, end_row)) threads.append(thread) thread.start() for thread in threads: thread.join() processed_img = img ``` 在这个例子,每个线程处理图像的一个垂直切片。每个线程的开始和结束行由图像的总行数和线程数计算得出。每个线程使用process_image_thread函数处理它的切片。最后,所有线程都完成后,将处理后的图像存储在processed_img变量。 注意,使用多线程并不总是比单线程更快,因为线程切换和同步开销可能会使得并行化变得更慢。因此,需要进行基准测试来确定最佳的线程数和处理方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值