0

i want to know how i can share Locks between python subprocesses, here are a simple example of what i want to make (which doesn't work:)

file 1

import posix_ipc
import pickle
import multiprocessing as mp
flag = (posix_ipc.O_CREAT | posix_ipc.O_TRUNC | posix_ipc.O_RDWR)

mem = posix_ipc.SharedMemory("Mem", flags=flag, size=200)
mem = mmap.mmap(mem.fd, 200, mmap.MAP_SHARED, mmap.PROT_WRITE)

m = mp.Manager()
lock = m.Lock()
B_lock = pickle.dumps(lock)
B_len=len(B_lock)
mem[0:B_len]=B_lock
print(B_len)
length=int(input("enter the length"))
lock=pickle.loads(mem[0:length])
print(lock)
input()

file 2

import posix_ipc
import multiprocessing as mp
import pickle

flag = 0

mem = posix_ipc.SharedMemory("Mem", flags=flag, size=200)

mem = mmap.mmap(mem.fd, 200, mmap.MAP_SHARED, mmap.PROT_WRITE)
length=int(input("enter the length"))
lock=pickle.loads(mem[0:length])
print(lock)

input()

the shared memory work perfectly but when ever i share the lock i get the following Error message

File "/usr/lib/python3.8/multiprocessing/connection.py", line 759, in answer_challenge raise AuthenticationError('digest sent was rejected') multiprocessing.context.AuthenticationError: digest sent was rejected

3
  • 1
    Did you tried named semaphore in posix_ipc? semanchuk.com/philip/posix_ipc/#semaphore Commented Dec 1, 2020 at 19:36
  • nope, i wanted to use locks in my project, but funny enough when i started searching about how to use posix_ipc semaphores, i found out about ilock Commented Dec 1, 2020 at 19:40
  • The interface of using ilock is also interesting. It is more pythonic and easier to use. Commented Dec 1, 2020 at 19:42

1 Answer 1

2

so thanks to this System-wide mutex in Python on Linux i got from it is what i am asking for is a system wide mutex, and you can achive it by using ilock, and this is my example

file 1

from ilock import ILock

print("start this process first")
lock = ILock("VoidLock")
with lock:
    print("now this process inside, run the other procsses")
    input("enter anything so the other procsses can get inside the lock")

print("the lock is relased")

input()

file 2

from ilock import ILock


lock = ILock("VoidLock")
print("now this process is witting")
with lock:
    print("now this process is inside ")
    input()

input()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.