SyncIntο
- class thread_factory.concurrency.sync_types.sync_int.SyncInt(*args, **kwargs)[source]ο
Bases:
ISyncπ Core Purposeο
SyncInt provides synchronized access to integer operations, ensuring that multiple threads can safely read and modify the value without race conditions.
π§ Featuresο
Full support for arithmetic operations (+, -, *, //, %, **, etc.)
Comparison operators (==, <, >, etc.)
Bitwise operations (&, |, ^, <<, >>)
Type conversion (int(), float(), str())
Manual and atomic value access via .get() and .set()
Safe thread-aware binary operations with locking on both operands
β‘ Power Operation Supportο
SyncInt includes custom implementations for __pow__ and __rpow__:
__pow__: Handles SyncInt ** x β supports optional modulo as a third argument.
__rpow__: Handles x ** SyncInt when x cannot handle the pow itself.
β οΈ Important Limitation (Python dispatch rule)ο
The built-in pow() function in Python always dispatches based on the left-most operand. This means:
β pow(SyncInt, β¦, β¦) β will invoke SyncInt.__pow__ β 3 ** SyncInt β will invoke SyncInt.__rpow__ (if int.__pow__ fails) β pow(3, SyncInt, 5) β SyncInt methods are NOT called (fails with TypeError)
To work around these dispatch limitations for ternary power (pow(a, b, c)), use:
SyncInt.safe_pow(a, b, c)
This static utility: - Unwraps all SyncInt arguments - Acquires all relevant locks (if needed) - Performs the operation correctly, regardless of argument order
π§ Deadlock Preventionο
For binary and ternary operations involving multiple SyncInt instances, locks are always acquired in order of object ID to avoid deadlocks.
This makes operations like SyncInt(3) ** SyncInt(4) % SyncInt(5) safe across threads.
- β Recommended Usage:
result = SyncInt.safe_pow(a, b, c) # Always works result = SyncInt(4) ** SyncInt(3) % 5 # Works if base is SyncInt result = 3 ** SyncInt(3) # Works (calls __rpow__) pow(3, SyncInt(3), 5) # β Will raise TypeError
- as_integer_ratio()[source]ο
Return a pair of integers, whose ratio is equal to the original int.
The ratio is in lowest terms and has a positive denominator.
>>> SyncInt(10).as_integer_ratio() (10, 1) >>> SyncInt(-10).as_integer_ratio() (-10, 1) >>> SyncInt(0).as_integer_ratio() (0, 1)
- bit_count()[source]ο
Number of ones in the binary representation of the absolute value of self.
Also known as the population count.
>>> bin(13) '0b1101' >>> SyncInt(13).bit_count() 3
- bit_length()[source]ο
Number of bits necessary to represent self in binary.
>>> bin(37) '0b100101' >>> SyncInt(37).bit_length() 6
- conjugate()[source]ο
Returns self, the complex conjugate of any int.
For all integers, this is just self.
- decrement(value=1)[source]ο
Atomically decrement the internal value by the specified amount.
- Parameters:
value (int or SyncInt) β The amount to subtract.
- Returns:
The updated value after decrement.
- Return type:
int
- property denominatorο
The denominator of the rational representation.
- Returns:
Denominator (always 1 for ints).
- Return type:
int
- classmethod from_bytes(b: bytes, byteorder: str, *, signed: bool = False)[source]ο
Return the integer represented by the given array of bytes.
- Parameters:
b (bytes) β The byte array to convert.
byteorder (str) β βbigβ or βlittleβ.
signed (bool) β Whether the integer is signed.
- Returns:
A new SyncInt representing the decoded value.
- Return type:
- get() int[source]ο
Return the current integer value in a thread-safe way.
- Returns:
The stored integer value.
- Return type:
int
- property imagο
The imaginary part of the number.
- Returns:
Always 0 for integers.
- Return type:
int
- increment(value=1)[source]ο
Atomically increment the internal value by the specified amount.
- Parameters:
value (int or SyncInt) β The amount to add.
- Returns:
The updated value after increment.
- Return type:
int
- is_integer()[source]ο
Always True. Exists for compatibility with float.
- Returns:
Always True for integers.
- Return type:
bool
- property numeratorο
The numerator of the rational representation.
- Returns:
Numerator.
- Return type:
int
- property realο
The real part of the number.
- Returns:
Real value (itself).
- Return type:
int
- static safe_pow(base, exp, mod=None)[source]ο
Thread-safe pow() that locks all SyncInt operands (if any) using consistent locking order.
- set(new_value: int)[source]ο
Set a new integer value in a thread-safe way.
- Parameters:
new_value (int) β The new value to assign.
- to_bytes(length: int, byteorder: str, *, signed: bool = False)[source]ο
Return an array of bytes representing an integer.
- Parameters:
length (int) β Desired byte length of the output.
byteorder (str) β βbigβ or βlittleβ.
signed (bool) β Whether to use twoβs complement.
- Returns:
The byte representation of the stored integer.
- Return type:
bytes