SingletonProcess
A SingletonProcess is a function that is run in a seperate process and only one exists at a time. This is useful for use facing functions that must run in the background and need to be shutdown automatically.
Always guard your code with if __name__ == '__main__' or multiprocessing will fail.
Note: This module handles one specific use case very well. In high performance applications or different use cases, you may be better off with a custom solution.
Examples
See examples for more examples.
Run two processes simultaneously (notice the unique process ID)
from SingletonProcess import SingletonProcess, block
from time import sleep
@SingletonProcess
def longComputation(x):
sleep(5)
return x ** 2
if __name__ == '__main__':
a = longComputation(1, pid='a')
b = longComputation(2, pid='b')
block()
print(a, b)
Stop new process automatically (notice pid=None, which acts as a wildcard and stops all processes)
from SingletonProcess import SingletonProcess, block
@SingletonProcess
def uniqueProcess():
while True:
print("Doing important stuff!")
if __name__ == '__main__':
uniqueProcess()
uniqueProcess() #stops execution of first process
block()
Use VBSingletonProcess and verbose = True to see
detailed info about internal proccesses.
from SingletonProcess import VBSingletonProcess, block, terminateProcessesByPID
from time import sleep
@VBSingletonProcess
def printList(l: list):
for item in l:
print(item)
sleep(1)
if __name__ == "__main__":
printList([1, 2, 3, 4, 5, 6], pid='a')
printList([10, 20, 30, 40, 50, 60], pid='b')
sleep(2.5)
printList(['a', 'b', 'c', 'd'])
sleep(2.5)
printList(['hello', 'hello', 'world', 'world'], pid='c')
sleep(2.5)
printList(['so', 'long', 'and', 'thanks', 'for', 'all', 'the', 'fish'], pid='c')
printList(range(0,100), pid='d')
block(pid='c', verbose=True)
terminateProcessesByPID(pid='d', verbose=True)
block(verbose=True)
Use different poolgroups to seperate out different types of tasks.
from SingletonProcess import SingletonProcess, block
class GroupA(SingletonProcess):
poolgroup = 'A'
class GroupB(SingletonProcess):
poolgroup = 'B'
@GroupA
def uniqueProcessA():
while True:
print("Doing important stuff!")
@GroupB
def uniqueProcessB():
while True:
print("Doing other important stuff!")
if __name__ == '__main__':
uniqueProcessA()
uniqueProcessB() #first process still runs
block(poolgroup='A')
block(poolgroup='B')
You can also override the getPID class method for custom use cases.
from SingletonProcess import SingletonProcess, block
from time import sleep
class SafeServer(SingletonProcess):
@staticmethod
def getPID(args, kwargs):
return kwargs['hostname']
@SafeServer
def startServer(hostname):
print("Starting server on hostname: ", hostname)
while True:
pass
if __name__ == '__main__':
startServer(hostname='https://examplea.com')
startServer(hostname='https://exampleb.com')
sleep(1)
startServer(hostname='https://examplea.com') #stops first server
block()
ThreadSafeSingletonProcess
Uses spawn instead of fork on linux, which may work better for projects that also use threads.
Metadata
Release files for SingletonProcess 0.1.5
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| singletonprocess-0.1.5.tar.gz | 6.9 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| singletonprocess-0.1.5-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 14.6 kB
Release files / singletonprocess-0.1.5.tar.gz
| Download URL | singletonprocess-0.1.5.tar.gz |
|---|---|
| Size | 6.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
5a5bccec561eec6c7f6f06142f7aa44a80880f159dbe554be87a3bff447e02df
|
|
BLAKE2b-256 checksum How to use checksums |
270873e2623cc7b325550f86238242b5d677df5f1f8ef0c45756445ad419024a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Release files / singletonprocess-0.1.5-py3-none-any.whl
| Download URL | singletonprocess-0.1.5-py3-none-any.whl |
|---|---|
| Size | 7.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
41fa1c2ab28c894af7ee006d880985303e4f0cf0e058b5a531bf76b9ba248bc9
|
|
BLAKE2b-256 checksum How to use checksums |
c1f61baf66bc0016ac00c00e6b3548d138ee37cbca4e8cac25d3dfce340f8bd4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Security for SingletonProcess 0.1.5
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.