A small example package
Project description
Thread-safe and process-safe singleton class. The singleton is shared between all threads. For every process, a new instance is initialized and shared between all threads of the process.
Examples
Basic usage:
from pyngleton import singleton
@singleton
class MyClass:
def __init__(self, x, y, z):
self.x = x
self.y = y
self.z = z
if __name__ == '__main__':
a = MyClass(1, 2, 3)
b = MyClass(4, 5, 6)
assert a is b
assert b.x == 1 and b.y == 2 and b.z == 3
Multithreading:
from concurrent.futures import ThreadPoolExecutor
from pyngleton import singleton
@singleton
class MyClass:
pass
if __name__ == '__main__':
n = 2
with ThreadPoolExecutor(max_workers=n) as exc:
tasks = [exc.submit(MyClass) for _ in range(n)]
results = set(task.result() for task in tasks)
assert len(results) == 1 # all threads share the same instance
Multiprocessing:
import multiprocessing
from concurrent.futures import ProcessPoolExecutor
from pyngleton import singleton
@singleton
class MyClass:
def __init__(self, x: int, y: int, z: int, queue: multiprocessing.Queue):
self.x = x
self.y = y
self.z = z
queue.put((x, y, z))
def worker(x: int, y: int, z: int, queue: multiprocessing.Queue):
MyClass(x, y, z, queue)
if __name__ == '__main__':
n = 2
queue = multiprocessing.Manager().Queue()
with ProcessPoolExecutor(max_workers=n) as exc:
tasks = [exc.submit(worker, i, i, i, queue) for i in range(n)]
for task in tasks:
task.result()
results = []
while not queue.empty():
results.append(queue.get())
assert results == [(0, 0, 0), (1, 1, 1)] # each process has its own instance
The classes decorated with @singleton
can't be pickled. If you want to be able to do that,
you can inherit from Singleton
or SingletonMeta
instead of using the decorator.
Inherting:
from pyngleton import Singleton, SingletonMeta
class MyClassA(metaclass=SingletonMeta):
pass
class MyClassB(Singleton):
pass
if __name__ == '__main__':
assert MyClassA() is MyClassA()
assert MyClassB() is MyClassB()
assert MyClassA() is not MyClassB()
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
pyngleton-0.0.6.tar.gz
(10.6 MB
view details)
Built Distribution
File details
Details for the file pyngleton-0.0.6.tar.gz
.
File metadata
- Download URL: pyngleton-0.0.6.tar.gz
- Upload date:
- Size: 10.6 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3c6d4d3e0fbbc2756a9c19afc7af6267a7070c8dbda668ed563a9ea44b1538b9 |
|
MD5 | 36569e8159a8453c003e2caf47d886c9 |
|
BLAKE2b-256 | b965eb7f0eb088cdfc8ed9a39f831e6049b148996704edf237aef18520abdc69 |
File details
Details for the file pyngleton-0.0.6-py3-none-any.whl
.
File metadata
- Download URL: pyngleton-0.0.6-py3-none-any.whl
- Upload date:
- Size: 3.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6a8ffdbddefc58f287098a579290fb7902f577b392949429e3c8bf050bd299af |
|
MD5 | 80f0aef2b973b117fca9493e68d10364 |
|
BLAKE2b-256 | d7dc1345cf550ef0a0d8d869519d03fab5b4a4f6eb2b855d18d0cd3c8a712dcf |