Allows existing functions to be run and stopped in seperate processes
Project description
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()
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for SingletonProcess-0.1.0rc0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0c0bbd9356bdfd0e8af1a20920c692d875a222fa57756def67df9fe6c06997cd |
|
MD5 | ff61b4b6d8b25fa7164d21325a6888a6 |
|
BLAKE2b-256 | 483f4d69c2b9358406bf56dbf135321cfdfa987a6ca925cfb89bc140245700d8 |
Hashes for SingletonProcess-0.1.0rc0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7c25e06ad6942f1badad1113c904d8b7c198162342550cfb201779c957369916 |
|
MD5 | 663f1804ca9b14c47283fb1b70bccf5c |
|
BLAKE2b-256 | 0125544c6d487070cf9992e26a65143c935d45edff83d199780d2f052eda1461 |