R. *_*ard 5 python nose python-2.7 python-multiprocessing
我有一个存储函数的类和一个尝试使用multiprocessing模块并行运行该函数的方法。我正在使用该nose模块测试该类,并且该nose.main()函数在运行时挂起(什么也没有发生,没有显示任何输出,必须手动告知内核停止并重新启动)。我的目录如下:
Parent/
test.py
Code/
__init__.py
code.py
tests/
test_code.py
Run Code Online (Sandbox Code Playgroud)
code.py 包含:
import multiprocessing as mp
import numpy as np
import itertools
def target(): pass
def target_star(args): return target(*args)
class FuncWrapper():
def __init__(self, fun):
self.fun = fun
def run(self, X):
try:
arg_list_objects = []
arg_list_inputs = []
for i in range(len(X)):
arg_list_objects.append(self.fun.im_self)
arg_list_inputs.append(X[i])
target.__code__ = self.fun.im_func.__code__
pool = mp.Pool(processes=mp.cpu_count()-1)
F = np.array(pool.map(target_star, itertools.izip(arg_list_objects, arg_list_inputs))).reshape(X.shape)
pool.close()
pool.join()
except:
F = self.fun(X)
return F
Run Code Online (Sandbox Code Playgroud)
try:块中的大多数代码用于使pool.map函数与类方法一起使用。
test_code.py 包含:
import numpy as np
from unittest import TestCase
import unittest
from Code.code import FuncWrapper
class TestCode(TestCase):
def f(self, x):
return x
def test_FuncWrapper(self):
x = np.random.uniform(0, 1, (50, 1))
return FuncWrapper(self.f).run(x)
if __name__ == '__main__':
unittest.main()
Run Code Online (Sandbox Code Playgroud)
test.py 包含:
import nose
nose.main()
Run Code Online (Sandbox Code Playgroud)
当我test_code.py从父文件夹运行时,它成功执行了测试,但是test.py运行时,什么也没有发生。我应该如何使用该nose.main函数使其不挂起,或者nose模块中是否还有另一个可以在此处使用的函数?
我正在使用带有Enthought Python发行版(Python 2.7.11 64位)的Ubuntu 14.04 LTS 64位及其Canopy IDE。