0

I am trying to work out the solution that a process would tell the other process that some values have changed.

import multiprocessing
import time
class Consumer(multiprocessing.Process):
 def __init__(self, share):
 super().__init__()
 self.share = share
 
 def run(self):
 print (self.share)
 self.share = "xxx"
 
share = "ssss"
A = Consumer(share)
B = Consumer(share)
if __name__ == '__main__':
 A = Consumer(share)
 A.start()
 time.sleep(5)
 B = Consumer(share)
 B.start()

expecting to have "xxx" to be printed when B runs. but got "ssss" as initial value.

after some researches, multiprocess.manager package can be used to achieve it. But due to the concerns of speed, i.e. 100 processes, with high frequency of accessing the share value, the lock would become a bottleneck.

Is there way to be able to lock the object when change the value but reading??

Gulzar
28.8k42 gold badges160 silver badges262 bronze badges
asked Apr 22, 2021 at 10:10
3
  • see pypi.org/project/readerwriterlock Commented Apr 22, 2021 at 10:43
  • 1
    how would you ever expect to see "xxx"? B.share is initialized to "ssss" on instantiation, and is totally separate from A.share. This wouldn't work even without threading or multiprocessing. Commented Apr 22, 2021 at 16:11
  • Also locks are very fast. If you want to guarantee concurrency safety, you need to use them. If you implement an efficient process, and you still find it's too slow, you may just need to use a different language. Commented Apr 22, 2021 at 16:18

1 Answer 1

2

Use a manager to share objects across processes:

import multiprocessing
import time
class Consumer(multiprocessing.Process):
 def __init__(self, manager_namespace):
 super().__init__()
 self.share = manager_namespace
 def run(self):
 print (self.share.myString)
 self.share.myString = "xxx"
if __name__ == '__main__':
 manager = multiprocessing.Manager()
 namespace = manager.Namespace()
 namespace.myString = 'sss'
 B = Consumer(namespace)
 A = Consumer(namespace)
 A.start()
 time.sleep(5)
 B = Consumer(namespace)
 B.start()

At least in my system it gives the required output.

answered Apr 23, 2021 at 8:58
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.