Skip to main content

Allows existing functions to be run and stopped in seperate processes

Project description

SingletonProcess

Tests Badge Python Version Badge License Badge

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.

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

singletonprocess-0.1.4.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

SingletonProcess-0.1.4-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

Details for the file singletonprocess-0.1.4.tar.gz.

File metadata

  • Download URL: singletonprocess-0.1.4.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for singletonprocess-0.1.4.tar.gz
Algorithm Hash digest
SHA256 b7ebffe33e128c78564b610b63ac1812a896888ba3108af6c63735366a023243
MD5 3f6f8aee7d6fc802aa6c037d49092481
BLAKE2b-256 d4619a6f7ad0c7b4ce437b9470b31875716c3506ab62c85ead550207fe095432

See more details on using hashes here.

File details

Details for the file SingletonProcess-0.1.4-py3-none-any.whl.

File metadata

File hashes

Hashes for SingletonProcess-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 46775f29c5aeb374f5c4c18dff823c1920549349e9130fd6a9b926505b4101f0
MD5 002a4a865372c1c8792b2a106b9a1256
BLAKE2b-256 fd9de045254c7025085e0451d1cc993fe187d677280105da6aeb082330b820c1

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page