Package: pytasking
PytaskingA simple library for Python 3.5+ that provides an easy interface for multitasking.Table of ContentsPytaskingTable of ContentsDependenciesInstallationSourcePyPiUsageAPIclass pytasking.Manager()add_task(task, *args, **kwargs)delete_task(t_id)get_task(t_id)get_tasks()add_proc(proc, *args, **kwargs)delete_proc(p_id)get_proc(p_id)get_procs()start()Known IssuesRecursive spawningPipe/Queue corruptionChangelog1.2Breaking Changes1.1.01.0.0DependenciesPython 3.5+There are no external module dependencies outside of the standard library however, if you'd like to take advantage ofuvloop, you can install that and thepytaskinglibrary will use it automatically (Only available on Linux/MacOS).InstallationSourceInclude the directorypytaskingin your project root directory.If on Linux/MacOS; runpython -m pip install -r requirements.txt.PyPiRunpip install pytasking.UsageA basic python example:#!/usr/bin/env pythonimportpytaskingimporttimedefhello(hello_queue):whileTrue:hello_queue.put_nowait("Hello World!")pytasking.sleep(1.5,sync=True)asyncdefping():whileTrue:try:print("Ping!")awaitpytasking.sleep(1.0)print("Pong!")exceptpytasking.asyncio.CancelledError:print("Pang!")breakasyncdefmain(task_manager):hellos=0hello_queue=pytasking.multiprocessing.Queue()hello_proc=task_manager.add_proc(hello,hello_queue)whileTrue:try:ifhellos==5:task_manager.delete_proc(hello_proc)ifhello_queue.qsize()>0:try:print(hello_queue.get_nowait())hellos+=1except:passping_task=task_manager.add_task(ping)awaitpytasking.sleep(0.5)task_manager.delete_task(ping_task)exceptpytasking.asyncio.CancelledError:breakif__name__=="__main__":task_manager=pytasking.Manager()task_manager.add_task(main,task_manager)try:task_manager.start()exceptKeyboardInterrupt:passexcept:raiseAPIclass pytasking.Manager()Instances of theManagerclass provide an asynchronous event loop to the program. Currently pytaskingonly supports 1 asynchronous event loopat any given time.Asynchronous tasks and parallel processes are spawned and managed by theManagerinstance.add_task(task, *args, **kwargs)Create an asynchronous task from a function definition. Pass arguments and keyword arguments as you would normally. This function returns an id from the has of the task. You can use the id to retrieve and delete the task. Make sure you define your function with the following template:asyncdefasynchronous_task_definition():# Define any arguments or keyword arguments as you normally would.# Do whatever you need to do here as you normally would.# If you want this task to run indefinitely, do this:whileTrue:try:# Do something forever.awaitpytasking.sleep(1.0)exceptpytasking.asyncio.CancelledError:# This one is important.# Normally you catch the cancel event and do something with it, but in this case, use it to break the loop and allow the task to close the task.breakexcept:raiseTasks will start immediately and you may add a task anytime.delete_task(t_id)Given a task id, you can call to delete a task. This method calls thecancel()method of the coroutine, it will give the coroutine the chance to cleanup and even deny the request if caught and handled in thepytasking.CancelledError.get_task(t_id)If you want to retrieve the underlying coroutine, you can use this method and provide the task id to get it.get_tasks()This will return all the task ids as a list, you can use this method in conjunction withget_task(t_id).add_proc(proc, *args, **kwargs)Create a parallel process from a function definition. Pass arguments and keyword arguments as you would normally. This function returns an id from the has of the process. You can use the id to retrieve and delete the process. Do note, by default the process runs sequentially. Try to follow this template:defparallel_process():# Define any arguments or keyword arguments as you normally would.# Do whatever you need to do here as you normally would.# If you want this task to run indefinitely, do this:whileTrue:try:# Do something forever.pytasking.sleep(1.0,sync=True)except:raisedelete_proc(p_id)Given a process id, you can call to delete a process. This method callsterminate()andjoin()to attempt to cleanly close the process. Closing the process while it is accessing a Pipe or Queue, may corrupt the resource.get_proc(p_id)If you want to retrieve the underlying process, you can use this method and provide the process id to get it.get_procs()This will return all the process ids as a list, you can use this method in conjunction withget_process(p_id).start()This begins theManagerinstance and starts all added tasks and processes.Known IssuesRecursive spawningThere maybe situations where you cannot spawn a task in a task, process in a process, task in a process, or a process in a task. I'll need to investigate further.Pipe/Queue corruptionIf you decide to delete a process be wary, if the process was in the middle of accessing a Queue or Pipe, that Queue or Pipe will be liable to corruption and will not be usable again.Changelog1.2Changing naming convention, moving toward 1.x convention.Breaking ChangesAll wrapped exceptions and data structures from theasyncioandmultiprocessingmodules have now been namespaced into pytasking. For example;pytasking.CancelledErroris nowpytasking.asyncio.CancelledError. This change is so that it is more explicit and natural.1.1.0Improved documentation.Implemented additional helper methods for the Manager class â€“ see the documentation for details.1.0.0This is the initial release of pytasking.