Package: pyaltt
pyalttthis library is deprecated and no longer supportedUseataskerlibrary insteadVarious tools for functions, looped and queued workers etc.License: Apache License 2.0FunctionCollectionCollects and executes a pack of functionsUsage example:frompyalttimportFunctionCollectonfc=FunctionCollecton()@fcdeff1():print('I am function 1')return1@fcdeff2():print('I am function 2')return2result=fc.run()# or fc(), returns dict containing all function resultsReal life example: definedumpfunction collection and calldump.run()or
simplydump()to collect registered modules information.Real life example 2: defineshutdownfunction collection and callshutdown()to stop threads of background workers.Parameters:on_errorfunction which is called if any function throws an exeption
(with e=Exception argument), or ifremovemethod is called and function
doesn't exist in collection.on_error_kwargsadditional arguments foron_errorinclude_exceptionsif function fails, value in result dict is set toNoneby default.  Specifyinginclude_exceptions=Truewill set value to
tuple (Exception, traceback)run()method actually just callsreturn self.execute(), so you can easily
override it. In addition to function result dict,execute()returnsTrueif
all functions were executed without an exceptions,Falseif not.defmy_run():print('Shutdown started')result,success=fc.execute()ifsuccess:print('Shutdown finished')else:print('Shutdown failed')returnresultfc.run=my_runBackground workersLooped worker decoratorSimple background worker which executes method in loop, with an interval/delay
if set.Usage example:frompyalttimportbackground_worker#transforms function into background worker which run in a loop@background_workerdefmyworker(**kwargs):print('I\'m a worker '+kwargs.get('worker_name'))myworker.start()# ................myworker.stop()Parameters for @background_worker:namealternative worker name (default is function name)daemonset background worker as daemon (default isTrue)oany object which worker can get withkwargs.get('o')on_errorfunction which is called if worker throws an exeption (with
e=Exception argument)on_error_kwargsadditional arguments foron_errordelay_beforedelay before each worker function executiondelay_after(or simplydelay) delay after each worker function
executionintervalsame asdelay_afterbut it will try to keep execution
interval exactlypoll_delaysleep precision (lower is better but uses CPU, default is 0.1
sec)realtimeUse different algorythm for sleeping to be even more exactNote: if running in virtual machine, unset "sync guest time with host"
otherwise real time sleep may work unpredictable.Poll delay should be lower or equal to delays. If you set very short delays,
don't forget to decrease poll delay as well.Real time loop execution in this library is not 100% exact and can't be used
e.g. in heavy industry. Real time loops require dedicated coding for the
particular task and for small delays can't be coded with Python at all or
require special tricks/hardware.Parameters for start():_daemonoverride initial params and set worker as daemon or notname, delay, interval and realtime parameters can be overriden in start (use
kwargs with_: _name, _delay, _interval etc.)All other parameters are passed to worker function (both args and kwargs)Worker gets all parameters used instart()plus additionally,worker_nameandoin kwargs (can be overriden instart()).Worker can accessselfobject viakwargs['_worker'].Paramterwait=Falsecan be used forstop()to send a signal to worker and
continue (default is wait until worker finish)Queue worker decoratorBackground worker which processes tasks in a queue.Usage example:frompyalttimportbackground_worker#transforms function into background worker which run on task in queue@background_worker(q=True)defmyworker(task,**kwargs):print('Got a task%s'%task)myworker.start()# ................myworker.put('task 1')# ................myworker.stop()Parameters:name,daemon,o,on_error,on_error_kwargssame as for
looped workerqueue_classuse alternative queue class (e.g. queue.PriorityQueue,
default isqueue,Queue).queueuse external queueIfqueueorqueue_classparameters are set,q=Trueis not required.Task can be any object (obvious). Worker has task always as first parameter.Parameters for start() / stop() are the same as for looped worker.Event worker decoratorBackground worker which runs method on event.frompyalttimportbackground_worker#transforms function into background worker which runs on event@background_worker(e=True)defmyworker(**kwargs):print('event triggered')myworker.start()# ................myworker.trigger()# ................myworker.stop()Parameters:name,daemon,o,on_error,on_error_kwargssame as for
looped workereventuse externalthreading.Event()object. If this parameter is set,e=Trueis not required.Parameters for start() / stop() are the same as for looped worker.Stopping loop from workerThis can be done in 2 ways: callingmyworker.terminate()from worker function
or simply returnFalse.Working directly with classesIf you define background workers in your classes which may be inherited or have
multiple objects, Background worker classes should be used without a wrapper.Class names:pyaltt.BackgroundWorkerpyaltt.BackgroundQueueWorkerpyaltt.BackgroundEventWorkerYou can overrideloopmethod to have own function executed when worker
starts.Loop callsrunfunction which's actually a worker function (the same
decorated/transformed in the examples above).(c) 2018-2019 Altertech Group,https://www.altertech.com/