Reputation: 108
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
Upvotes: 0
Views: 615
Reputation: 108
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()
Upvotes: 2