Skip to content

Bug: elabapi-python fails to initialize in Pyodide/JupyterLite due to _multiprocessing dependency #38

@vfreysz

Description

@vfreysz

Description

The elabapi-python library cannot be used in web-based Python environments like Pyodide or JupyterLite. The initialization of elabapi_python.ApiClient fails because it attempts to instantiate multiprocessing.pool.ThreadPool.

The multiprocessing module is not available in Pyodide/WASM environments, which leads to a ModuleNotFoundError. This makes it impossible to use the library for client-side applications running in the browser.

Steps to Reproduce

The following minimal code, when run in a JupyterLite notebook or a Pyodide environment, will reproduce the error:


%pip install -q elabapi-python
import elabapi_python
from elabapi_python.rest import ApiException

# Initialize a configuration object
configuration = elabapi_python.Configuration()
configuration.host = "https://your.elab.instance/api/v2"
API_KEY = "your_api_key"

# The following line triggers the error
api_client = elabapi_python.ApiClient(configuration)

api_client.set_default_header(header_name='Authorization', header_value=API_KEY)

Expected Behavior

The ApiClient should initialize without errors, allowing the library to be used in a browser-based environment. A possible solution could be to avoid using ThreadPool or to have a fallback mechanism for environments where multiprocessing is not supported.

Actual Behavior

The code fails with the following traceback:

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Cell In[1], line 11
      8 API_KEY = "your_api_key"
     10 # The following line triggers the error
---> 11 api_client = elabapi_python.ApiClient(configuration)
     13 api_client.set_default_header(header_name='Authorization', header_value=API_KEY)

File /lib/python3.12/site-packages/elabapi_python/api_client.py:68, in ApiClient.__init__(self, configuration, header_name, header_value, cookie)
     66 self.configuration = configuration
     67 # Use a thread pool for asynchronous requests
---> 68 self.pool = ThreadPool()
     69 self.rest_client = rest.RESTClientObject(configuration)
     70 self.default_headers = {}

File /lib/python3.12/multiprocessing/pool.py:1018, in ThreadPool.__init__(self, processes, initializer, initargs)
   1017 def __init__(self, processes=None, initializer=None, initargs=()):
-> 1018     Pool.__init__(self, processes, initializer, initargs)

File /lib/python3.12/multiprocessing/pool.py:212, in Pool.__init__(self, processes, initializer, initargs, maxtasksperchild, context)
    210 self._taskqueue = queue.SimpleQueue()
    211 self._cache = {}
--> 212 self._change_notifier = self._ctx.SimpleQueue()
    213 self._maxtasksperchild = maxtasksperchild
    214 self._wrap_exception = True

File /lib/python3.12/multiprocessing/context.py:114, in BaseContext.SimpleQueue(self)
    112 def SimpleQueue(self):
    113     '''Returns a queue object'''
--> 114     from .queues import SimpleQueue
    115     return SimpleQueue(ctx=self.get_context())

File /lib/python3.12/multiprocessing/queues.py:23
     19 import errno
     21 from queue import Empty, Full
---> 23 import _multiprocessing
     25 from . import connection
     26 from . import context

ModuleNotFoundError: No module named '_multiprocessing'

Environment

  • Python via Pyodide (in JupyterLite)
  • elabapi-python version installed via %pip install elabapi-python

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions