atomic api Example with multiprocessing and multiple threads::ΒΆ

You need the following steps to utilize the module:

  1. create function used by child processes, refer to UIntAPIs, IntAPIs, BytearrayAPIs, StringAPIs, SetAPIs, ListAPIs, in each process, you can create multiple threads. On macOS, before the main process start, please set environment variable OBJC_DISABLE_INITIALIZE_FORK_SAFETY=YES, to alliviate Python bug

    def process_run(a):

         def subthread_run(a):

             a.array_sub_and_fetch(b'\x0F')

         threadlist = []

         for t in range(5000):

             threadlist.append(Thread(target=subthread_run, args=(a,)))

         for t in range(5000):

             threadlist[t].start()

         for t in range(5000):

             threadlist[t].join()

  2. create the shared bytearray

        a = atomic_bytearray(b'ab', length=7, paddingdirection='r', paddingbytes=b'012', mode='m')

  3. start processes/threads to utilize the shared bytearray

        processlist = []

        for p in range(2):

            processlist.append(Process(target=process_run, args=(a,)))

        for p in range(2):

            processlist[p].start()

        for p in range(2):

            processlist[p].join()

        assert a.value == int.to_bytes(27411031864108609,length=8,byteorder='big')