Python multiprocessing: Permission denied

PythonLinux

Python Problem Overview


I'm getting an error when trying to execute python program that uses multiprocessing package:

  File "/usr/local/lib/python2.6/multiprocessing/__init__.py", line 178, in RLock
    return RLock()
  File "/usr/local/lib/python2.6/multiprocessing/synchronize.py", line 142, in __init__
    SemLock.__init__(self, RECURSIVE_MUTEX, 1, 1)
  File "/usr/local/lib/python2.6/multiprocessing/synchronize.py", line 49, in __init__
    sl = self._semlock = _multiprocessing.SemLock(kind, value, maxvalue)
OSError: [Errno 13] Permission denied

It looks like the user doesn't have permission to access shared memory. When executing with root privileges it works fine.

Is there any solution to run it as normal user(not root)?

Python version 2.6.2 , OS is Linux 2.6.18 (CentOS release 5.4) and it's VPS machine.

Python Solutions


Solution 1 - Python

For POSIX semaphores to work, the users need r/w access to shared memory (/dev/shm).

Check the permissions to /dev/shm. On my laptop (Ubuntu) it looks like this:

$ ls -ld /dev/shm
drwxrwxrwt 2 root root          40 2010-01-05 20:34 shm

To permanently set the correct permissions (even after a reboot), add the following to your /etc/fstab:

none /dev/shm tmpfs rw,nosuid,nodev,noexec 0 0

Haven't tried this, just copied from a forum post.

Solution 2 - Python

In my OVH VPS Classic, this error was caused by a loop in /dev/shm and /run/shm. Both were symlinks linking to each other. So as root here is what I did:

# rm /dev/shm
# mkdir /dev/shm
# chmod 777 /dev/shm
# nano /etc/fstab

Then I modified the shm line from:

none /dev/shm tmpfs rw 0 0

To:

none /dev/shm tmpfs rw,nosuid,nodev,noexec 0 0

Restarted the server... And that fixed the problem! Alternatively you can mount shm manually:

# mount /dev/shm

Hope this helps :-)

Solution 3 - Python

One simple solution without rebooting is

sudo chmod 777 /dev/shm

That solved my problem.

Solution 4 - Python

I tried all the recommendations related to chmod and shm, but in my case the solution was:

Using conda navigator:
  • In base-environment run (in order to see the navigator):
    $ anaconda-navigator
    
  • Create a new conda environment: from the button CREATE in the navigator
  • Select the new environment with your mouse
  • Install "notebook": Install it from anaconda-navigator in the new environment
Using command line:
  • Create a new anaconda enviroment (enviroment name "my_new_env"):
    $ conda create --name my_new_env
    
  • Enter to my_new_env:
    $ conda activate my_new_env
    
  • Install Jupyter notebook:
    $ conda install jupyter-core (OR $ conda install notebook)
    

As a summary, don't use snap to install Jupyter notebook.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRoman DolgiyView Question on Stackoverflow
Solution 1 - PythoncodeapeView Answer on Stackoverflow
Solution 2 - PythonGabriel HautclocqView Answer on Stackoverflow
Solution 3 - PythonQin HeyangView Answer on Stackoverflow
Solution 4 - PythonJogabellView Answer on Stackoverflow