Title: Parallel import of modules causes a ModuleNotFoundError · Issue #130094 · python/cpython · GitHub
Open Graph Title: Parallel import of modules causes a ModuleNotFoundError · Issue #130094 · python/cpython
X Title: Parallel import of modules causes a ModuleNotFoundError · Issue #130094 · python/cpython
Description: Bug report Bug description: Using the free-threading build parallel import of modules causes a ModuleNotFoundError. The module that cannot be found can differ. A minimal reproducer: import sys from threading import Thread, Barrier from i...
Open Graph Description: Bug report Bug description: Using the free-threading build parallel import of modules causes a ModuleNotFoundError. The module that cannot be found can differ. A minimal reproducer: import sys from...
X Description: Bug report Bug description: Using the free-threading build parallel import of modules causes a ModuleNotFoundError. The module that cannot be found can differ. A minimal reproducer: import sys from...
Opengraph URL: https://github.com/python/cpython/issues/130094
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Parallel import of modules causes a ModuleNotFoundError","articleBody":"# Bug report\n\n### Bug description:\n\nUsing the free-threading build parallel import of modules causes a `ModuleNotFoundError`. The module that cannot be found can differ. A minimal reproducer:\n```python\nimport sys\nfrom threading import Thread, Barrier\nfrom importlib import import_module, reload\nimport time\n\n\n# Setup threads\nt0 = time.perf_counter()\nnumber_of_threads = 8\nbarrier = Barrier(number_of_threads)\n\npmods = []\n\n\ndef work(ii):\n # Method doing the actual import\n barrier.wait()\n while True:\n try:\n m = pmods.pop()\n mod = import_module(m)\n # print(f' {ii}: {m} done')\n except IndexError:\n return\n\n\nworker_threads = []\nfor ii in range(number_of_threads):\n worker_threads.append(Thread(target=work, args=[ii]))\n\ndt = time.perf_counter() - t0\nprint(f\"setup threads {dt*1e3:.2f}\")\n\n\ndef parallel_import(modules: list[str]):\n global pmods\n pmods += modules\n for t in worker_threads:\n t.start()\n for t in worker_threads:\n t.join()\n\n\ndef seq_import(modules: list[str]):\n for m in modules:\n mod = import_module(m)\n\n\nimport_method = seq_import\nimport_method = parallel_import\n\nmods = [\n \"abc\",\n \"functools\",\n \"weakref\",\n \"linecache\",\n \"glob\",\n \"annotationlib\",\n \"argparse\",\n \"fnmatch\",\n \"itertools\",\n \"operator\",\n \"string\",\n \"re\",\n \"collections\",\n \"sqlite3\",\n \"pathlib\",\n \"urllib\",\n \"typing\",\n \"csv\",\n \"uuid\",\n]\n\nmods += [\n \"setuptools\",\n \"sympy\",\n \"django\",\n \"boto3\",\n] # these are some packages that need to be installed. without these the reproduction is the issue is much harder\n\nt0 = time.perf_counter()\nimport_method(mods)\ndt = time.perf_counter() - t0\nprint(f\"{import_method.__name__} {dt*1e3:.2f}\")\n```\nNote: this script fails on my system about 1 in 4 times. Changing the modules imported (or the order, or the number of threads) can effect this.\n\nModules that have given issues: `collections.abs`, `glob`, `sympy`, `django.utils.regex_helper`. This suggests it is a general issue, and not a particular package.\n\n\n### CPython versions tested on:\n\nCPython main branch\n\n### Operating systems tested on:\n\nWindows\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-130101\n* gh-155915\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/eendebakpt","@type":"Person","name":"eendebakpt"},"datePublished":"2025-02-13T19:49:05Z","url":"https://github.com/python/cpython/issues/130094"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react-reconciler,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@d5750093facf |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:99b017ae-d5d6-ef65-a901-a7af74a11eea |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8486:182799:5F39BF:7F0FC4:6AED1E07 |
| html-safe-nonce | 7c65e4439ca5c85e85c2b15bbfd93d43fdb48556b8f027794cc963df6c17d047 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4NDg2OjE4Mjc5OTo1RjM5QkY6N0YwRkM0OjZBRUQxRTA3IiwidmlzaXRvcl9pZCI6IjYzODk4MjEwNDM2MTM5NjU4MzEiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 24b09af0b62205bb4c2a3db9539735ccbe01ca9b7c754a155b0db1847d3d333a |
| hovercard-subject-tag | issue:2851960100 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/130094/issue_layout |
| twitter:image | https://opengraph.githubassets.com/411b2a46f6b56516d833ab095c1ca6dbb183e5b73517d3117cda8ed2c243c389/python/cpython/issues/130094 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/411b2a46f6b56516d833ab095c1ca6dbb183e5b73517d3117cda8ed2c243c389/python/cpython/issues/130094 |
| og:image:alt | Bug report Bug description: Using the free-threading build parallel import of modules causes a ModuleNotFoundError. The module that cannot be found can differ. A minimal reproducer: import sys from... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | eendebakpt |
| hostname | github.com |
| expected-hostname | github.com |
| None | e3e0f7f63e50c6fe879198a93a0a023a648ecc0a5e9bc3b5e033ac57172237e6 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 707a24b346ac8a82878b9c084efaa38a997660cb |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width