Package: pool-workers
Pool_WorkersPool_Workers is a small package for dealing with pools, workers and queues.Installation:pipinstallpool-workersMore info:Usefull functions for Pool as well as Worker."""Default params:Pool(max_workers=os.cpu_count() + 4, name=None, queue=None, wait_queue=True,result_queue=None, workers_sleep=0.1, callback=None, execption_handler=execption_handler)"""frompool_workersimportPoolpool=Pool(...)pool.start()# Start all workers to process queue tasks.pool.is_alive()# Return true if is there any alive worker, else false.pool.is_idle()# Return true if is there any worker in idle mode, else false.pool.is_done()# Return true if the queue is empty (no tasks left to process).pool.is_paused()# Return true if the all workers have been paused, else false.pool.shutdown()# Abort all workerspool.join()# Wait for all workers to finish the all queue tasks.pool.result()# return a list resultpool.pause()# pause the all workerspool.resume()# resume the all workerspool.count()# count workerspool.update()# adjust the number of workers"""default params:Worker(name, queue, result=None, wait_queue=False, sleep=0.5, callback=None,execption_handler=execption_handler)"""frompool_workersimportWorkerworker=Worker(...)worker.start()worker.abort()# stop worker in a safe wayworker.aborted()worker.pause()worker.paused()worker.resume()# And like a normal thread, worker has also:worker.is_alive()worker.join()UsageExample 1:importtimeimportthreadingimportrandomfromqueueimportQueuefrompool_workersimportPool,Task# Our logic to be performed Asynchronously.defour_process(a):t=threading.current_thread()# just to semulate how mush time this logic is going to take to be done.time.sleep(random.uniform(0,3))print(f'{t.getName()}is finished the task{a}...')# Our function to handle thrown exceptions from 'our_process' logic.defexecption_handler(thread_name,exception):print(f'{thread_name}:{exception}')# create a queue & pool.q=Queue()pool=Pool(name='Pool_1',queue=q,wait_queue=False,execption_handler=execption_handler)# adding some tasks the the queue.foriinrange(10):# Creating task with args and kwargs and push it into the queue.task=Task(our_process,args=(i,),kwargs={})q.put(task)try:# start the Poolpool.start()# go back to the main thread from time to another to check the KeyboardInterruptwhilepool.is_alive():pool.join(0.5)except(KeyboardInterrupt,SystemExit):# shutdown the pool by aborting its Workers/threads.pool.shutdown()"""output resultWorker_1_Pool_1 is finished the task 1 ...Worker_1_Pool_1 is finished the task 2 ...Worker_0_Pool_1 is finished the task 0 ...Worker_0_Pool_1 is finished the task 4 ...Worker_0_Pool_1 is finished the task 5 ...Worker_1_Pool_1 is finished the task 3 ...Worker_0_Pool_1 is finished the task 6 ...Worker_1_Pool_1 is finished the task 7 ...Worker_0_Pool_1 is finished the task 8 ...Worker_0_Pool_1: The Queue is empty.Worker_1_Pool_1 is finished the task 9 ...Worker_1_Pool_1: The Queue is empty.Worker_0_Pool_1 is stoppedWorker_1_Pool_1 is stoppedPool_1 is shutted down"""Example 2:importtimeimportthreadingimportrandomfromqueueimportQueuefrompool_workersimportWorker,Task# Our logic to be performed Asynchronously.defour_process(a):t=threading.current_thread()# just to semulate how mush time this logic is going to take to be done.time.sleep(random.uniform(0,3))print(f'{t.getName()}is finished the task{a}...')# Our function to handle thrown exceptions from 'our_process' logic.defexecption_handler(thread_name,exception):print(f'{thread_name}:{exception}')# create a queue & pool.q=Queue()t=Worker(name='worker',queue=q,wait_queue=False,sleep=0.1,execption_handler=execption_handler)# adding some tasks the the queue.foriinrange(10):# Creating task with args and kwargs and push it into the queue.task=Task(our_process,args=(i,),kwargs={})q.put(task)try:# start the Poolt.start()# block the code execution here to check the KeyboardInterrupt (to stop the worker safely)whilet.is_alive():t.join(0.5)# Can't go here until the worker finishes his work.except(KeyboardInterrupt,SystemExit):# stop the Worker/thread.t.abort()"""output resultworker is finished the task 0 ...worker is finished the task 1 ...worker is finished the task 2 ...worker is finished the task 3 ...worker is finished the task 4 ...worker is finished the task 5 ...worker is finished the task 6 ...worker is finished the task 7 ...worker is finished the task 8 ...worker is finished the task 9 ..."""LicenseMIT License