Skip to content

[JOSS] Can not use multiprocessing #82

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
malmans2 opened this issue Jun 21, 2023 · 7 comments
Closed

[JOSS] Can not use multiprocessing #82

malmans2 opened this issue Jun 21, 2023 · 7 comments

Comments

@malmans2
Copy link

Describe the bug
I'm not able to run this: https://nctoolkit.readthedocs.io/en/latest/parallel.html#parallel-processing-using-multiprocessing
I tried both from jupyterlab and from ipython.

To Reproduce

import nctoolkit as nc
nc.options(parallel = True)

def process_chain(infile, outfile):
    ds = nc.open_data(ff)
    ds.assign(tos = lambda x: x.sst + 273.15)
    ds.tmean()
    ds.to_nc(outfile)

ensemble = nc.create_ensemble("data/ensemble")
import multiprocessing
pool = multiprocessing.Pool(2)
for ff in ensemble:
    pool.apply_async(process_chain, [ff, ff.replace("ensemble", "new")])
pool.close()
pool.join()

where data/ensemble is this.

I get the following (truncated here because it's too long for GH issues), and then it just hangs:

Process SpawnPoolWorker-1:
Traceback (most recent call last):
Process SpawnPoolWorker-2:
Traceback (most recent call last):
  File "/Users/mattia/mambaforge/envs/nctoolkit/lib/python3.11/multiprocessing/process.py", line 314, in _bootstrap
    self.run()
  File "/Users/mattia/mambaforge/envs/nctoolkit/lib/python3.11/multiprocessing/process.py", line 108, in run
    self._target(*self._args, **self._kwargs)
  File "/Users/mattia/mambaforge/envs/nctoolkit/lib/python3.11/multiprocessing/pool.py", line 114, in worker
    task = get()
           ^^^^^
  File "/Users/mattia/mambaforge/envs/nctoolkit/lib/python3.11/multiprocessing/queues.py", line 367, in get
    return _ForkingPickler.loads(res)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: Can't get attribute 'process_chain' on <module '__main__' (built-in)>
  File "/Users/mattia/mambaforge/envs/nctoolkit/lib/python3.11/multiprocessing/process.py", line 314, in _bootstrap
    self.run()
  File "/Users/mattia/mambaforge/envs/nctoolkit/lib/python3.11/multiprocessing/process.py", line 108, in run
    self._target(*self._args, **self._kwargs)
  File "/Users/mattia/mambaforge/envs/nctoolkit/lib/python3.11/multiprocessing/pool.py", line 114, in worker
    task = get()
           ^^^^^
  File "/Users/mattia/mambaforge/envs/nctoolkit/lib/python3.11/multiprocessing/queues.py", line 367, in get
    return _ForkingPickler.loads(res)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: Can't get attribute 'process_chain' on <module '__main__' (built-in)>
Process SpawnPoolWorker-3:
Traceback (most recent call last):
  File "/Users/mattia/mambaforge/envs/nctoolkit/lib/python3.11/multiprocessing/process.py", line 314, in _bootstrap
    self.run()
  File "/Users/mattia/mambaforge/envs/nctoolkit/lib/python3.11/multiprocessing/process.py", line 108, in run
    self._target(*self._args, **self._kwargs)
  File "/Users/mattia/mambaforge/envs/nctoolkit/lib/python3.11/multiprocessing/pool.py", line 114, in worker
    task = get()
           ^^^^^
  File "/Users/mattia/mambaforge/envs/nctoolkit/lib/python3.11/multiprocessing/queues.py", line 367, in get
    return _ForkingPickler.loads(res)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: Can't get attribute 'process_chain' on <module '__main__' (built-in)>

Desktop (please complete the following information):

  • OS: macOS

Additional context

# packages in environment at /Users/mattia/mambaforge/envs/nctoolkit:
#
# Name                    Version                   Build  Channel
anyio                     3.7.0              pyhd8ed1ab_1    conda-forge
appnope                   0.1.3              pyhd8ed1ab_0    conda-forge
argon2-cffi               21.3.0             pyhd8ed1ab_0    conda-forge
argon2-cffi-bindings      21.2.0          py311h5547dcb_3    conda-forge
asttokens                 2.2.1              pyhd8ed1ab_0    conda-forge
async-lru                 2.0.2              pyhd8ed1ab_0    conda-forge
attrs                     23.1.0             pyh71513ae_1    conda-forge
babel                     2.12.1             pyhd8ed1ab_1    conda-forge
backcall                  0.2.0              pyh9f0ad1d_0    conda-forge
backports                 1.0                pyhd8ed1ab_3    conda-forge
backports.functools_lru_cache 1.6.4              pyhd8ed1ab_0    conda-forge
beautifulsoup4            4.12.2             pyha770c72_0    conda-forge
bleach                    6.0.0              pyhd8ed1ab_0    conda-forge
blosc                     1.21.4               heccf04b_0    conda-forge
bokeh                     3.1.1              pyhd8ed1ab_0    conda-forge
brotli                    1.0.9                hb7f2c08_8    conda-forge
brotli-bin                1.0.9                hb7f2c08_8    conda-forge
bzip2                     1.0.8                h0d85af4_4    conda-forge
c-ares                    1.19.1               h0dc2134_0    conda-forge
ca-certificates           2023.5.7             h8857fd0_0    conda-forge
cairo                     1.16.0            h09dd18c_1016    conda-forge
cartopy                   0.21.1          py311hb21c73c_1    conda-forge
cdo                       2.2.0                h9d92bdb_4    conda-forge
certifi                   2023.5.7           pyhd8ed1ab_0    conda-forge
cffi                      1.15.1          py311ha86e640_3    conda-forge
cftime                    1.6.2           py311hd5badaa_1    conda-forge
charset-normalizer        3.1.0              pyhd8ed1ab_0    conda-forge
colorama                  0.4.6              pyhd8ed1ab_0    conda-forge
colorcet                  3.0.1              pyhd8ed1ab_0    conda-forge
comm                      0.1.3              pyhd8ed1ab_0    conda-forge
contourpy                 1.1.0           py311h5fe6e05_0    conda-forge
curl                      8.1.2                hbee3ae8_0    conda-forge
cycler                    0.11.0             pyhd8ed1ab_0    conda-forge
debugpy                   1.6.7           py311h814d153_0    conda-forge
decorator                 5.1.1              pyhd8ed1ab_0    conda-forge
defusedxml                0.7.1              pyhd8ed1ab_0    conda-forge
dill                      0.3.6              pyhd8ed1ab_1    conda-forge
eccodes                   2.30.2               hecd1f2b_0    conda-forge
entrypoints               0.4                pyhd8ed1ab_0    conda-forge
esmf                      8.4.2           mpi_mpich_h39d0f2c_100    conda-forge
exceptiongroup            1.1.1              pyhd8ed1ab_0    conda-forge
executing                 1.2.0              pyhd8ed1ab_0    conda-forge
expat                     2.5.0                hf0c8a7f_1    conda-forge
fftw                      3.3.10          nompi_h4fa670e_108    conda-forge
findlibs                  0.0.5              pyhd8ed1ab_0    conda-forge
flit-core                 3.9.0              pyhd8ed1ab_0    conda-forge
font-ttf-dejavu-sans-mono 2.37                 hab24e00_0    conda-forge
font-ttf-inconsolata      3.000                h77eed37_0    conda-forge
font-ttf-source-code-pro  2.038                h77eed37_0    conda-forge
font-ttf-ubuntu           0.83                 hab24e00_0    conda-forge
fontconfig                2.14.2               h5bb23bf_0    conda-forge
fonts-conda-ecosystem     1                             0    conda-forge
fonts-conda-forge         1                             0    conda-forge
fonttools                 4.40.0          py311h2725bcf_0    conda-forge
freetype                  2.12.1               h3f81eb7_1    conda-forge
fribidi                   1.0.10               hbcb3906_0    conda-forge
geos                      3.11.2               hf0c8a7f_0    conda-forge
gettext                   0.21.1               h8a4c099_0    conda-forge
graphite2                 1.3.13            h2e338ed_1001    conda-forge
gsl                       2.7                  h93259b0_0    conda-forge
harfbuzz                  7.3.0                h413ba03_0    conda-forge
hdf4                      4.2.15               h9804679_6    conda-forge
hdf5                      1.14.0          mpi_mpich_h18663c6_3    conda-forge
holoviews                 1.16.2             pyhd8ed1ab_0    conda-forge
hvplot                    0.8.4              pyhd8ed1ab_1    conda-forge
icu                       72.1                 h7336db1_0    conda-forge
idna                      3.4                pyhd8ed1ab_0    conda-forge
importlib-metadata        6.7.0              pyha770c72_0    conda-forge
importlib_metadata        6.7.0                hd8ed1ab_0    conda-forge
importlib_resources       5.12.0             pyhd8ed1ab_0    conda-forge
ipykernel                 6.23.1             pyh736e0ef_0    conda-forge
ipython                   8.14.0             pyhd1c38e8_0    conda-forge
jasper                    4.0.0                h794afb9_1    conda-forge
jedi                      0.18.2             pyhd8ed1ab_0    conda-forge
jinja2                    3.1.2              pyhd8ed1ab_1    conda-forge
json5                     0.9.5              pyh9f0ad1d_0    conda-forge
jsonschema                4.17.3             pyhd8ed1ab_0    conda-forge
jupyter-lsp               2.2.0              pyhd8ed1ab_0    conda-forge
jupyter_client            8.2.0              pyhd8ed1ab_0    conda-forge
jupyter_core              5.3.1           py311h6eed73b_0    conda-forge
jupyter_events            0.6.3              pyhd8ed1ab_0    conda-forge
jupyter_server            2.6.0              pyhd8ed1ab_0    conda-forge
jupyter_server_terminals  0.4.4              pyhd8ed1ab_1    conda-forge
jupyterlab                4.0.2              pyhd8ed1ab_0    conda-forge
jupyterlab_pygments       0.2.2              pyhd8ed1ab_0    conda-forge
jupyterlab_server         2.23.0             pyhd8ed1ab_0    conda-forge
kiwisolver                1.4.4           py311hd2070f0_1    conda-forge
krb5                      1.20.1               h049b76e_0    conda-forge
lcms2                     2.15                 h2dcdeff_1    conda-forge
lerc                      4.0.0                hb486fe8_0    conda-forge
libaec                    1.0.6                hf0c8a7f_1    conda-forge
libblas                   3.9.0           17_osx64_openblas    conda-forge
libbrotlicommon           1.0.9                hb7f2c08_8    conda-forge
libbrotlidec              1.0.9                hb7f2c08_8    conda-forge
libbrotlienc              1.0.9                hb7f2c08_8    conda-forge
libcblas                  3.9.0           17_osx64_openblas    conda-forge
libcurl                   8.1.2                hbee3ae8_0    conda-forge
libcxx                    16.0.6               hd57cbcb_0    conda-forge
libdeflate                1.18                 hac1461d_0    conda-forge
libedit                   3.1.20191231         h0678c8f_2    conda-forge
libev                     4.33                 haf1e3a3_1    conda-forge
libexpat                  2.5.0                hf0c8a7f_1    conda-forge
libffi                    3.4.2                h0d85af4_5    conda-forge
libgfortran               5.0.0           11_3_0_h97931a8_31    conda-forge
libgfortran5              12.2.0              he409387_31    conda-forge
libglib                   2.76.3               hc62aa5d_0    conda-forge
libiconv                  1.17                 hac89ed1_0    conda-forge
libjpeg-turbo             2.1.5.1              hb7f2c08_0    conda-forge
liblapack                 3.9.0           17_osx64_openblas    conda-forge
libnetcdf                 4.9.2           mpi_mpich_h4b56550_5    conda-forge
libnghttp2                1.52.0               he2ab024_0    conda-forge
libopenblas               0.3.23          openmp_h429af6e_0    conda-forge
libpng                    1.6.39               ha978bb4_0    conda-forge
libsodium                 1.0.18               hbcb3906_1    conda-forge
libsqlite                 3.42.0               h58db7d2_0    conda-forge
libssh2                   1.11.0               hd019ec5_0    conda-forge
libtiff                   4.5.1                hf955e92_0    conda-forge
libuuid                   2.38.1               hb7f2c08_0    conda-forge
libwebp-base              1.3.0                hb7f2c08_0    conda-forge
libxcb                    1.15                 hb7f2c08_0    conda-forge
libxml2                   2.11.4               hd95e348_0    conda-forge
libzip                    1.9.2                h6db710c_1    conda-forge
libzlib                   1.2.13               h8a1eda9_5    conda-forge
linkify-it-py             2.0.0              pyhd8ed1ab_0    conda-forge
llvm-openmp               16.0.6               hff08bdf_0    conda-forge
lz4-c                     1.9.4                hf0c8a7f_0    conda-forge
magics                    4.13.0               hb1c45c1_5    conda-forge
magics-python             1.5.8              pyhd8ed1ab_1    conda-forge
markdown                  3.4.3              pyhd8ed1ab_0    conda-forge
markdown-it-py            2.2.0              pyhd8ed1ab_0    conda-forge
markupsafe                2.1.3           py311h2725bcf_0    conda-forge
matplotlib-base           3.7.1           py311h2bf763f_0    conda-forge
matplotlib-inline         0.1.6              pyhd8ed1ab_0    conda-forge
mdit-py-plugins           0.4.0              pyhd8ed1ab_0    conda-forge
mdurl                     0.1.0              pyhd8ed1ab_0    conda-forge
metpy                     1.5.0              pyhd8ed1ab_0    conda-forge
mistune                   3.0.0              pyhd8ed1ab_0    conda-forge
mpi                       1.0                       mpich    conda-forge
mpich                     4.1.1              hd33e60e_100    conda-forge
multiprocess              0.70.14         py311h5547dcb_3    conda-forge
munkres                   1.1.4              pyh9f0ad1d_0    conda-forge
nbclient                  0.8.0              pyhd8ed1ab_0    conda-forge
nbconvert-core            7.6.0              pyhd8ed1ab_0    conda-forge
nbformat                  5.9.0              pyhd8ed1ab_0    conda-forge
nco                       5.1.6                h0317220_0    conda-forge
ncplot                    0.3.4           py311h9bdd199_0    conda-forge
nctoolkit                 0.9.4                     dev_0    <develop>
ncurses                   6.4                  hf0c8a7f_0    conda-forge
nest-asyncio              1.5.6              pyhd8ed1ab_0    conda-forge
netcdf-fortran            4.6.1           mpi_mpich_h2786e36_0    conda-forge
netcdf4                   1.6.4           nompi_py311h6222480_100    conda-forge
notebook-shim             0.2.3              pyhd8ed1ab_0    conda-forge
numpy                     1.25.0          py311hc44ba51_0    conda-forge
openjpeg                  2.5.0                h13ac156_2    conda-forge
openssl                   3.1.1                h8a1eda9_1    conda-forge
overrides                 7.3.1              pyhd8ed1ab_0    conda-forge
packaging                 23.1               pyhd8ed1ab_0    conda-forge
pandas                    2.0.2           py311hab14417_0    conda-forge
pandocfilters             1.5.0              pyhd8ed1ab_0    conda-forge
panel                     1.1.0              pyhd8ed1ab_2    conda-forge
pango                     1.50.14              hbce5e75_1    conda-forge
parallelio                2.5.10          mpi_mpich_hda3997c_102    conda-forge
param                     1.13.0             pyh1a96a4e_0    conda-forge
parso                     0.8.3              pyhd8ed1ab_0    conda-forge
pcre2                     10.40                h1c4e4bc_0    conda-forge
pexpect                   4.8.0              pyh1a96a4e_2    conda-forge
pickleshare               0.7.5                   py_1003    conda-forge
pillow                    9.5.0           py311h7cb0e2d_1    conda-forge
pint                      0.22               pyhd8ed1ab_1    conda-forge
pip                       23.1.2             pyhd8ed1ab_0    conda-forge
pixman                    0.40.0               hbcb3906_0    conda-forge
pkgutil-resolve-name      1.3.10             pyhd8ed1ab_0    conda-forge
platformdirs              3.6.0              pyhd8ed1ab_0    conda-forge
pooch                     1.7.0              pyha770c72_3    conda-forge
proj                      9.2.1                hc8d59c9_0    conda-forge
prometheus_client         0.17.0             pyhd8ed1ab_0    conda-forge
prompt-toolkit            3.0.38             pyha770c72_0    conda-forge
prompt_toolkit            3.0.38               hd8ed1ab_0    conda-forge
psutil                    5.9.5           py311h5547dcb_0    conda-forge
pthread-stubs             0.4               hc929b4f_1001    conda-forge
ptyprocess                0.7.0              pyhd3deb0d_0    conda-forge
pure_eval                 0.2.2              pyhd8ed1ab_0    conda-forge
pycparser                 2.21               pyhd8ed1ab_0    conda-forge
pyct                      0.4.6                      py_0    conda-forge
pyct-core                 0.4.6                      py_0    conda-forge
pygments                  2.15.1             pyhd8ed1ab_0    conda-forge
pyobjc-core               9.2             py311hf110eff_0    conda-forge
pyobjc-framework-cocoa    9.2             py311hf110eff_0    conda-forge
pyparsing                 3.1.0              pyhd8ed1ab_0    conda-forge
pyproj                    3.6.0           py311h854a015_1    conda-forge
pyrsistent                0.19.3          py311h5547dcb_0    conda-forge
pyshp                     2.3.1              pyhd8ed1ab_0    conda-forge
pysocks                   1.7.1              pyha2e5f31_6    conda-forge
python                    3.11.4          h30d4d87_0_cpython    conda-forge
python-dateutil           2.8.2              pyhd8ed1ab_0    conda-forge
python-fastjsonschema     2.17.1             pyhd8ed1ab_0    conda-forge
python-json-logger        2.0.7              pyhd8ed1ab_0    conda-forge
python-tzdata             2023.3             pyhd8ed1ab_0    conda-forge
python_abi                3.11                    3_cp311    conda-forge
pytz                      2023.3             pyhd8ed1ab_0    conda-forge
pyviz_comms               2.3.2              pyhd8ed1ab_0    conda-forge
pyyaml                    6.0             py311h5547dcb_5    conda-forge
pyzmq                     25.1.0          py311h5dacc12_0    conda-forge
readline                  8.2                  h9e318b2_1    conda-forge
requests                  2.31.0             pyhd8ed1ab_0    conda-forge
rfc3339-validator         0.1.4              pyhd8ed1ab_0    conda-forge
rfc3986-validator         0.1.1              pyh9f0ad1d_0    conda-forge
scipy                     1.10.1          py311h16c3c4d_3    conda-forge
send2trash                1.8.2              pyhd1c38e8_0    conda-forge
setuptools                67.7.2             pyhd8ed1ab_0    conda-forge
shapely                   2.0.1           py311heb7bb94_1    conda-forge
simplejson                3.19.1          py311h5547dcb_0    conda-forge
six                       1.16.0             pyh6c4a22f_0    conda-forge
snappy                    1.1.10               h225ccf5_0    conda-forge
sniffio                   1.3.0              pyhd8ed1ab_0    conda-forge
soupsieve                 2.3.2.post1        pyhd8ed1ab_0    conda-forge
sqlite                    3.42.0               h2b0dec6_0    conda-forge
stack_data                0.6.2              pyhd8ed1ab_0    conda-forge
tempest-remap             2.2.0                he127c65_0    conda-forge
terminado                 0.17.1             pyhd1c38e8_0    conda-forge
tinycss2                  1.2.1              pyhd8ed1ab_0    conda-forge
tk                        8.6.12               h5dbffcc_0    conda-forge
tomli                     2.0.1              pyhd8ed1ab_0    conda-forge
tornado                   6.3.2           py311h2725bcf_0    conda-forge
tqdm                      4.65.0             pyhd8ed1ab_1    conda-forge
traitlets                 5.9.0              pyhd8ed1ab_0    conda-forge
typing-extensions         4.6.3                hd8ed1ab_0    conda-forge
typing_extensions         4.6.3              pyha770c72_0    conda-forge
typing_utils              0.1.0              pyhd8ed1ab_0    conda-forge
tzdata                    2023c                h71feb2d_0    conda-forge
uc-micro-py               1.0.1              pyhd8ed1ab_0    conda-forge
udunits2                  2.2.28               h06ef574_0    conda-forge
urllib3                   2.0.3              pyhd8ed1ab_0    conda-forge
wcwidth                   0.2.6              pyhd8ed1ab_0    conda-forge
webencodings              0.5.1                      py_1    conda-forge
websocket-client          1.6.0              pyhd8ed1ab_0    conda-forge
wheel                     0.40.0             pyhd8ed1ab_0    conda-forge
xarray                    2023.5.0           pyhd8ed1ab_0    conda-forge
xorg-libxau               1.0.11               h0dc2134_0    conda-forge
xorg-libxdmcp             1.1.3                h35c211d_0    conda-forge
xyzservices               2023.5.0           pyhd8ed1ab_1    conda-forge
xz                        5.2.6                h775f41a_0    conda-forge
yaml                      0.2.5                h0d85af4_2    conda-forge
zeromq                    4.3.4                he49afe7_1    conda-forge
zipp                      3.15.0             pyhd8ed1ab_0    conda-forge
zlib                      1.2.13               h8a1eda9_5    conda-forge
zstd                      1.5.2                hbc0c0cd_6    conda-forge

openjournals/joss-reviews#5494

@robertjwilson
Copy link
Member

Thanks for spotting this. I changed the nctoolkit internals for macOS 2 weeks ago to fix this bug on macOS: #29

But I forgot to update this doc.

Essentially, nctoolkit now uses multiprocess when running in parallel on macOS, but multiprocessing on Linux, due to weird differences in behaviour introduced in Python 3.7. So the example needs to use multiprocess on macOS. I've just added this to the docs: https://nctoolkit.readthedocs.io/en/latest/parallel.html

@malmans2
Copy link
Author

OK, multiprocess does not crash like before.

But I think I'm misunderstanding this example.

This is copied and pasted from the documentation.

import nctoolkit as nc
nc.options(parallel = True)

def process_chain(infile, outfile):
    ds = nc.open_data(ff)
    ds.assign(tos = lambda x: x.sst + 273.15)
    ds.tmean()
    ds.to_nc(outfile)

ensemble = nc.create_ensemble("data/ensemble")
import multiprocess as mp
pool = mp.Pool(2)
for ff in ensemble:
    pool.apply_async(process_chain, [ff, ff.replace("ensemble", "new")])
pool.close()
pool.join()

Is this assertion correct? Because it fails on my machine, so data/new does not exits.

import os
assert os.path.exists("data/new")

Also, is this correct? ds = nc.open_data(ff), or should it be ds = nc.open_data(infile)?
I tried both of them, but nothing fails and I don't see the new directory.

@malmans2
Copy link
Author

malmans2 commented Jun 21, 2023

Ahhh I see, I need to create the directory new myself, then it gets populated.
It's worth mentioning it in the documentation, and I guess you need to fix the bug in the function (ff vs infile).

But I think there's still a problem because there are silent failures.

@robertjwilson
Copy link
Member

I've modified the example so it generates directories and the function is now fixed https://nctoolkit.readthedocs.io/en/latest/parallel.html

@malmans2
Copy link
Author

I don't think it's fixed. The new directory has the wrong path.
You are creating the directory "./new" and writing to "../../data/new".

As I mentioned in my previous comment, isn't it an issue that the code silently fails?

@robertjwilson
Copy link
Member

OK. Git screw up. I thought a fix was commited and pushed but it wasn't.

Now definitely fixed https://nctoolkit.readthedocs.io/en/latest/parallel.html

If this is still failing silently can you confirm the OS/Python?

@malmans2
Copy link
Author

malmans2 commented Aug 3, 2023

The code works now.

Still, if I add a bug on purpose in the function, no Exception is raised (python 3.11, macOS).
It's not relevant for JOSS though, so I'm closing this.

@malmans2 malmans2 closed this as completed Aug 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants