On Sep 9, 2019, at 01:23, Vinay Sharma <vinay04sha...@icloud.com> wrote:

> Also, I didn't mean simple variables to be atomic. I just mean that some 
> objects which are frequently being used across processes in multiprocessing 
> can have some atomic operations/methods.

It sounds like all you want is an automatically-synchronizing 
multiprocessing.Value, then?

That has very little in common with C++ atomic, so let’s just forget that whole 
bit.

Presumably you know that multiprocessing.Value already has a lock parameter, 
which does what you want for simple stores and loads. The only thing that’s 
missing is a way to lock combined operations, like +=.

It sounds like you also want to be able to call update methods and operators 
directly on the Value rather than on its value property, but that actually 
makes it easier.

You can build this yourself pretty easily. The cleanest way is probably to 
delegate to a non-synchronized Value. Something like this:

    class SyncValue:
        def __init__(self, typ, value, lock=None):
            if lock is None: lock = mp.RLock()
            self._value = mp.Value(typ, value)
            self._lock = lock
        @property
        def value(self):
            with self._lock:
                return self._value.value
        # etc.
        def __iadd__(self, other):
            with self._lock:
                self._value.value += other
            return self
        # likewise for other operators and methods

And now your code becomes:

    import time
    from multiprocessing import Process
    from mpsyncvalue import SyncValue 

    def func(val):
       for i in range(50):
       time.sleep(0.01)
       val.value += 1

    if __name__ == '__main__':
       v = SyncValue('i', 0) 
       procs = [Process(target=func, args=(v,)) for i in range(10)]

       for p in procs: p.start()
       for p in procs: p.join()

       print(v.value)

I don’t think this is a great design, but if you want it, build it, share it on 
PyPI, and if other people like it and want it added to the stdlib, then it 
doesn’t really matter what I think.

I suspect you’re going to want a few iterations on the API (and on working out 
exactly what’s needed for unit tests) before it’s ready for the stdlib anyway. 
(Remember, once it’s in stdlib, it’s a lot harder to change the API–and you 
have to wait a year and a half to use the change, and often even longer to 
force all of your users/deployments to upgrade.)

_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/YDTHJVRM2SMUXHRW6LM77BSMIFN5JC7Q/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to