Python 测试使用多处理的代码时,nose.main挂起
我有一个存储函数的类和一个尝试使用Python 测试使用多处理的代码时,nose.main挂起,python,python-2.7,python-multiprocessing,nose,Python,Python 2.7,Python Multiprocessing,Nose,我有一个存储函数的类和一个尝试使用多处理模块并行运行函数的方法。我正在用nose模块测试这个类,并且nose.main()函数在运行时挂起(没有发生任何事情,没有显示输出,必须手动告诉内核停止并重新启动)。我的目录看起来像: Parent/ test.py Code/ __init__.py code.py tests/ test_code.py code.py包含: import multiprocessing as m
多处理
模块并行运行函数的方法。我正在用nose
模块测试这个类,并且nose.main()
函数在运行时挂起(没有发生任何事情,没有显示输出,必须手动告诉内核停止并重新启动)。我的目录看起来像:
Parent/
test.py
Code/
__init__.py
code.py
tests/
test_code.py
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
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()
import nose
nose.main()
try:
块中的大部分代码用于使pool.map
函数与类方法一起工作
test\u 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
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()
import nose
nose.main()
test.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
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()
import nose
nose.main()
当我从父文件夹运行test\u code.py
时,它成功地执行了测试,但当运行test.py
时,什么也没有发生。我应该如何使用nose.main
函数使其不会挂起,或者nose
模块中是否有其他函数可以在此处工作
我正在使用Ubuntu14.04 LTS 64位和Enthound Python发行版(Python2.7.11 64位)以及它们的Canopy IDE。可能与