Pre-PEP: Safe Parallel Python

I’d like to share my perspective regarding the proposed Safe Parallel Python API, specifically the use of dunder methods like freeze and mutex.

From a usability standpoint, exposing dunder methods directly to developers might be confusing. In Python, dunders are usually reserved for interpreter-level protocols, and users typically do not call them directly.

A more Pythonic approach could be to provide a library-level API or context managers. For example:

from concurrent.mutex import Mutex

counter = 0
mu = Mutex()

with mu.lock():
  counter += 1

For built-in data structures, I would propose creating concurrent-safe versions, similar to what we have in Java:

  • ConcurrentList
  • ConcurrentQueue
  • CopyOnWriteList
  • ConcurrentSet
  • ConcurrentDeque
  • AtomicInt
  • AtomicBool
  • AtomicRef
  • BoundedQueue
  • ConcurrentSortedDict / ConcurrentSortedSet

Maybe all of these could be part of a concurrent module in the standard library.
Explicit is better than implicit: adding thread-safety methods directly to built-in types creates implicit behavior, whereas providing separate concurrent-safe types allows the user to choose explicitly which type to use.


For user-defined types and functions, instead of protect:

from concurrent.sync import synchronized_class, synchronized_method

@synchronized_class
class ConcurrentClass:

    @synchronized_method
    def concurrent_method(self):
        ...

Or, in my opinion, a more explicit syntax:

sync class ConcurrentClass:
    sync def concurrent_method(self):
        …
  • sync class would mark all methods in the class as automatically synchronized with an internal lock.
  • sync def could be used for individual methods of any class.
    This makes thread safety explicit and obvious, rather than relying on users to manually add locks in each method.

Otherwise i can show the table of examples :

Keyword Example
sync sync class Counter:
sync def increment(self):
locked locked class ConcurrentList:
locked def append(self, x):
atomic atomic class Counter:
atomic def increment(self):
safe safe class SharedList:
safe def append(self, x):
guarded guarded class BankAccount:
guarded def transfer(self, amount):
protected protected class SharedDict:
protected def set_item(self, k, v):

Instead of __freeze__ for class attributes:

class Point:    
    final x: int = 0    
    final y: int = 0

Again, this follows the principle that explicit is better than implicit.

Maybe i forgot something, thank you for attention