Skip to main content

Pytasking

A simple library for Python 3.5+ that provides an easy interface for multitasking.

Table of Contents

Dependencies

  • 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).

Installation

Source

  • Include the directory pytasking in your project root directory.
  • If on Linux/MacOS; run python -m pip install -r requirements.txt.

PyPi

  • Run pip install pytasking.

Usage

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

API

class pytasking.Manager()

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 Manager instance.

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.

delete_task(t_id)

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 pytasking.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 with get_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:

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

delete_proc(p_id)

Given a process id, you can call to delete a process. This method calls terminate() and join() 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 with get_process(p_id).

start()

This begins the Manager instance and starts all added tasks and processes.

Known Issues

Recursive spawning

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.

Pipe/Queue corruption

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.

Changelog

1.2

  • Changing naming convention, moving toward 1.x convention.

Breaking Changes

  • All wrapped exceptions and data structures from the asyncio and multiprocessing modules have now been namespaced into pytasking. For example; pytasking.CancelledError is now pytasking.asyncio.CancelledError. This change is so that it is more explicit and natural.

1.1.0

  • Improved documentation.
  • Implemented additional helper methods for the Manager class – see the documentation for details.

1.0.0

  • This is the initial release of pytasking.

Metadata

Release files for pytasking 1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytasking 1.2
File Size Uploaded Provenance Actions
pytasking-1.2.tar.gz 5.8 kB Without provenance Details for pytasking-1.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pytasking 1.2
File Interpreter ABI Platform Actions
pytasking-1.2-py3-none-any.whl Python 3 none any Details for pytasking-1.2-py3-none-any.whl

Total release size: 11.5 kB

Release files / pytasking-1.2.tar.gz

Download URL pytasking-1.2.tar.gz
Size 5.8 kB
Tags Source
SHA-256 checksum
How to use checksums
45b42edd805af651151256eea0b15937a6003f36d7b256da5614a004c862e2d2
BLAKE2b-256 checksum
How to use checksums
e096cc48dd61078331d2a60cf8251687bb456bf9422325d972392bc716ffdab6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.40.0 CPython/3.8.0

Release files / pytasking-1.2-py3-none-any.whl

Download URL pytasking-1.2-py3-none-any.whl
Size 5.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7de35e3780eea6075742a736db168ec1b4f6677d77fa51e6e5f19f74ba21cfaf
BLAKE2b-256 checksum
How to use checksums
fe46d643ac10a79a615cde08834c08397c110c486207e73d68fbc36f54df9340
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.40.0 CPython/3.8.0

Security for pytasking 1.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page