A multitasking library for Python 3.5+
A simple library for Python 3.5+ that provides an easy interface for multitasking.
Table of Contents
- Table of Contents
- Known Issues
- Python 3.5+
There are no external module dependencies outside of the standard library however, if you'd like to take advantage of
uvloop, you can install that and the
pytasking library will use it automatically (Only available on Linux/MacOS).
- Include the directory
pytaskingin your project root directory.
- If on Linux/MacOS; run
python -m pip install -r requirements.txt.
pip install pytasking.
A basic python example:
#!/usr/bin/env python import pytasking import time def hello(hello_queue): while True: hello_queue.put_nowait("Hello World!") pytasking.sleep(1.5, sync=True) async def ping(): while True: try: print("Ping!") await pytasking.sleep(1.0) print("Pong!") except pytasking.asyncio.CancelledError: print("Pang!") break async def main(task_manager): hellos = 0 hello_queue = pytasking.multiprocessing.Queue() hello_proc = task_manager.add_proc(hello, hello_queue) while True: try: if hellos == 5: task_manager.delete_proc(hello_proc) if hello_queue.qsize() > 0: try: print(hello_queue.get_nowait()) hellos += 1 except: pass ping_task = task_manager.add_task(ping) await pytasking.sleep(0.5) task_manager.delete_task(ping_task) except pytasking.asyncio.CancelledError: break if __name__ == "__main__": task_manager = pytasking.Manager() task_manager.add_task(main, task_manager) try: task_manager.start() except KeyboardInterrupt: pass except: raise
Instances of the
Manager class provide an asynchronous event loop to the program. Currently pytasking only supports 1 asynchronous event loop at any given time.
Asynchronous tasks and parallel processes are spawned and managed by the
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:
async def asynchronous_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: while True: try: # Do something forever. await pytasking.sleep(1.0) except pytasking.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. break except: raise
Tasks will start immediately and you may add a task anytime.
Given a task id, you can call to delete a task. This method calls the
cancel() method of the coroutine, it will give the coroutine the chance to cleanup and even deny the request if caught and handled in the
If you want to retrieve the underlying coroutine, you can use this method and provide the task id to get it.
This will return all the task ids as a list, you can use this method in conjunction with
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:
def parallel_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: while True: try: # Do something forever. pytasking.sleep(1.0, sync=True) except: raise
Given a process id, you can call to delete a process. This method calls
join() to attempt to cleanly close the process. Closing the process while it is accessing a Pipe or Queue, may corrupt the resource.
If you want to retrieve the underlying process, you can use this method and provide the process id to get it.
This will return all the process ids as a list, you can use this method in conjunction with
This begins the
Manager instance and starts all added tasks and processes.
There 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.
If 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.
- Changing naming convention, moving toward 1.x convention.
- All wrapped exceptions and data structures from the
multiprocessingmodules have now been namespaced into pytasking. For example;
pytasking.asyncio.CancelledError. This change is so that it is more explicit and natural.
- Improved documentation.
- Implemented additional helper methods for the Manager class â€“ see the documentation for details.
- This is the initial release of pytasking.
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
|Filename, size||File type||Python version||Upload date||Hashes|
|Filename, size pytasking-1.2-py3-none-any.whl (5.7 kB)||File type Wheel||Python version py3||Upload date||Hashes View|
|Filename, size pytasking-1.2.tar.gz (5.8 kB)||File type Source||Python version None||Upload date||Hashes View|