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

void
  • 98
  • 8
  • Did you tried named semaphore in posix_ipc? http://semanchuk.com/philip/posix_ipc/#semaphore – Ali Tou Dec 01 '20 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 – void Dec 01 '20 at 19:40
  • The interface of using ilock is also interesting. It is more pythonic and easier to use. – Ali Tou Dec 01 '20 at 19:42

1 Answers1

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()
void
  • 98
  • 8