testing python multiprocessing pool code with nose

2024/11/17 23:44:41

I am trying to write tests with nose that get set up with something calculated using multiprocessing.

I have this directory structure:

code/tests/tests.py

tests.py looks like this:

import multiprocessing as mpdef f(i):return i ** 2pool = mp.Pool()
out = pool.map(f, range(10))def test_pool():"""Really simple test that relies on the output of pool.map.The actual tests are much more complicated, but this is allthat is needed to produce the problem."""ref_out = map(f, range(10))assert out == ref_outif __name__ == '__main__':test_pool()

Running from the code directory, python tests/tests.py passes.

nosetests tests/tests.py fails to complete. It starts up, but never gets through the call to pool.mapand just hangs.

Why is this and what is the simplest solution?

Answer

The problem is related to the fact that pool.map is called at the "global level". Normally you want to avoid that, because these statements will be executed even if your file is simply imported.

Nose has to import your module to be able to find your tests and later execute them, therefore I believe the problem happens while the import mechanism kicks in (I haven't spent time trying to find out the exact reason for this behaviour)

You should move your initialization code to a test fixture instead; Nose supports fixtures with the with_setup decorator. Here is one possibility (probably the simplest change while keeping pool and out as globals):

import multiprocessing as mp
from nose import with_setuppool = None
out  = Nonedef f(i):return i ** 2def setup_func():global poolglobal outpool = mp.Pool()out  = pool.map(f, range(10))@with_setup(setup_func)
def test_pool():"""Really simple test that relies on the output of pool.map.The actual tests are much more complicated, but this is allthat is needed to produce the problem."""global outref_out = map(f, range(10))assert out == ref_outif __name__ == '__main__':test_pool()

Executing:

$ nosetests tests/tests.py
.
----------------------------------------------------------------------
Ran 1 test in 0.011sOK
https://en.xdnf.cn/q/71128.html

Related Q&A

Python verify url goes to a page

I have a list of urls (1000+) which have been stored for over a year now. I want to run through and verify them all to see if they still exist. What is the best / quickest way to check them all and re…

Bokeh: Synchronizing hover tooltips in linked plots

I have two linked plots. When hovering, I would like to have a tooltip appear in both plots. I already use the linked selection with great success, but now I want to link the tooltips also.Below is an …

Pipe STDIN to a script that is itself being piped to the Python interpreter?

I need to implement an SVN pre-commit hook which executes a script that itself is stored in SVN.I can use the svn cat command to pipe that script to the Python interpreter, as follows:svn cat file://$R…

subprocess.call using cygwin instead of cmd on Windows

Im programming on Windows 7 and in one of my Python projects I need to call bedtools, which only works with Cygwin on Windows. Im new to Cygwin, installed the default version + everything needed for be…

Django Celery Received unregistered task of type appname.tasks.add

Following the documentation and the Demo Django project here https://github.com/celery/celery/tree/3.1/examples/djangoProject Structurepiesup2|piesup2| |__init__.py| |celery.py| |settings.py| |urls…

Documenting and detailing a single script based on the comments inside

I am going to write a set of scripts, each independent from the others but with some similarities. The structure will most likely be the same for all the scripts and probably looks like: # -*- coding: …

Using Ansible variables in testinfra

Using TestInfra with Ansible backend for testing purposes. Everything goes fine except using Ansible itself while running teststest.pyimport pytest def test_zabbix_agent_package(host):package = host.pa…

How to create a dictionary of dictionaries of dictionaries in Python

So I am taking a natural language processing class and I need to create a trigram language model to generate random text that looks "realistic" to a certain degree based off of some sample da…

How to separate Master Slave (DB read / writes) in Flask Sqlalchemy

Im trying to separate the Read and write DB operations via Flask Sqlalchemy. Im using binds to connect to the mysql databases. I would want to perform the write operation in Master and Reads from slave…

Why import class from another file will call __init__ function?

The structure of the project is:project - main.py - session.py - spider.pyThere is a class in session.py:import requestsclass Session:def __init__(self):self.session = requests.Session()print(Session c…