首页 > 解决方案 > Pytest and concurrent.futures interacting strangely

问题描述

I am using pytest to test my project, and I am using concurrent.futures to add parallelism to some of my algorithms. When I use pytest without the parallelized code, the tests complete as expected. When I run the parallelized code directly (without pytest), the code executes as expected. However, when I try to run pytest on parallelized code, some lines importing my project modules, such as

import tools_2d.classes

throw the exception

AttributeError: module 'tools_2d' has no attribute 'classes'

Thus, pytest and concurrent.futures seem to be working when used independently, but throw exceptions when used together. It seems that the workers spawned by multiprocessing cannot find my project modules when invoked by pytest. What am I doing wrong?

I am using python 3.9 on Windows. My project has the following structure:

repo
|-- tools_2d
|   |-- algorithms.py
|   |-- classes.py
|   |-- drawing.py
|   `-- utils.py
|-- algorithms
|   |-- line.py
|   `-- region.py
|-- tests
|   `-- test_algorithms.py
|-- main.py
|-- storage.py
|-- requirements.txt
`-- ...

标签: pythonpytestconcurrent.futures

解决方案


推荐阅读