Package: offspring
This is a collection of objects and patterns for working with processes in Python using the multiprocessing library.The main idea is that you express your unit of work as a simple method on an object and then when that object is
instantiated the work will be run in a subprocess.Use casesOffspring was built to address the following use cases for running code in a subprocess.Run something oncefromoffspringimportSubprocessclassMyTask(Subprocess):definit(self,arg1):# This is run in the parent process and is used to prepare your object.# It receives whatever arguments were supplied to the constructor.self.arg1=arg1defrun(self):# This will be run in the child process and completes your work.# ...MyTask('this is arg1').wait()Run in a loopfromoffspringimportSubprocessLoopclassMyTask(SubprocessLoop):definit(self,arg1):# This is the same as init for Subprocess.self.arg1=arg1defbegin(self):# Called before the start of the loop in your child process.# ...defloop(self):# Called each loop iteration in your your child process.# It can return a sleep duration until the next loop, or False to stop the loop.# ...defend(self):# Called after the end of the loop, before termination in your child process.# ...MyTask('this is arg1').wait()Implementation details.init(*args,**kwargs)Called when an instance of your class is created.  It receives the same arguments as the__init__method, so you are
encouraged to explicitly define the arguments you expect..start()Creates the subprocess.  This is automatically called unless you setEXPLICIT_STARTtoTrue..wait()If you need to wait for your child process you can call.waiton yourSubprocessobject.  This is just a
shortcut to.joinon themultiprocessing.Processobject..shutdown()This will send aTERMsignal to the child process, unlessTERMINATE_ON_SHUTDOWNisFalse, and then calls.wait()to join the child process.  It is automatically called whenever the parent process exits via theatexitmodule..processEachSubprocessobject has a.processattribute that is themultiprocessing.Processobject.WAIT_FOR_CHILDDefaults toFalse.If set toTrueon yourSubprocessclass then aPipewill be used to block the parent process until the child
has started.  This is useful when you want to ensure that yourSubprocessobject is started and.runis called
even if the parent process exits quickly.classMyTask(Subprocess):WAIT_FOR_CHILD=Truedefrun(self):print("This will always print")MyTask()TheSubprocessLoopclass does this to ensure that your object hasbegin&endcalled (loopmay not be
called as a TERM signal received during startup will prevent the loop from every actually completing other thanbegin&end).TERMINATE_ON_SHUTDOWNDefaults toTrue.If set toFalsethen when.shutdownis called on aSubprocessobject the child processwill notbe
terminated before being joined.  This means that the parent will block until the child completes the.runfunction.importtimeclassMyTask(Subprocess):TERMINATE_ON_SHUTDOWN=Falsedefrun(self):time.sleep(2)MyTask()EXPLICIT_STARTDefaults toFalse.If set toTruethen when you instantiate an object you must explicitly call.start()before the child process
will be spawned.classMyTask(Subprocess):EXPLICIT_START=Truedefrun(self):print("Running!")task=MyTask()# Do some other worktask.start()# Running! is now printed