Skip to main content

asynctasklist

PyPI Tests Changelog License

a simple tasklist library for running tasks asynchronously or synchronously

Installation

Install this library using pip:

pip install asynctasklist

Basic TaskList Usage

from asynctasklist import TaskList, Task
import time

# initialize the tasklist variable
tasks = TaskList()

# template function to return a new task
def makeTask(timeout, callback):
    startTime = 0 # initialize startTime variable

    def init():
        nonlocal startTime
        startTime = round(time.time() * 1000) # set the start time to the time when this task is first run

    def task():
        if round(time.time() * 1000) - startTime >= timeout: # check if timeout duration has passed
            callback() # run some code
            return True # return True since to task is done
        return False # return False since to task is not done
    return Task(init=init, task=task) # return a new task

# define callback function
def message():
    print("timeout is done")
    pass

# add a new task to the list
tasks.add(makeTask(1000, message)) # print message after 1 second
tasks.add(makeTask(2000, message)) # print message 2 seconds after the first message
pt = ParallelTask()
pt.add(makeTask(1000, message))
pt.add(makeTask(2000, message))
tasks.add(pt) # print message 1 second and 2 seconds after second message

# the timeline for the above tasklist would be as follows:
# wait 1sec --> message1 --> wait 2sec --> message2 --> wait 1sec --> message3 --> wait 1sec --> message3

# if you want to run the tasklist asynchronously, call the start_async method
tasks.start_async()

# alternatively, you can run the tasklist in a thread if you want more control
task_thread = tasks.start_async_thread()


# if you want to run the tasklist alongside your main program, simply put `tasks.execute()` in the main program loop
# main program loop
while True:
    # run main app code
    app.run() # example code
    # update gui
    gui.update() # example code

    # run the tasklist
    tasks.execute()
    if tasks.isDone():
        print("all tasks are done")
  • notes:
    • lambdas can be used in place of functions for inline task initialization
  • ParallelTasks function the same as TaskLists, but instead of running the task one at a time in the order they were added, it runs all tasks at the same time.
    • it is recommended to have a check at the beginning of each task in a ParallelTask to make sure work needs to be done before running the task in case some tasks take longer to finish than the others

how to build locally

git clone https://github.com/moojor224/asynctasklist.git
cd asynctasklist
python3 -m pip wheel ./

Metadata

Release files for asynctasklist 0.3

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

Source distribution (sdist)

Source distribution for asynctasklist 0.3
File Size Uploaded
asynctasklist-0.3.tar.gz 7.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for asynctasklist 0.3
File Interpreter ABI Platform
asynctasklist-0.3-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 15.2 kB

Release files / asynctasklist-0.3.tar.gz

Download URL asynctasklist-0.3.tar.gz
Size 7.5 kB
Tags Source
SHA-256 checksum
How to use checksums
6fa2f9e548b6d4a34bd6a5f379d4396973519293fd6eb35720dc8112936194a8
BLAKE2b-256 checksum
How to use checksums
fb68b67b95724ec4e9e4490ed87e2e801661b4688830a55a1fea9f824053d23e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.1.0 CPython/3.12.4

Release files / asynctasklist-0.3-py2.py3-none-any.whl

Download URL asynctasklist-0.3-py2.py3-none-any.whl
Size 7.7 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
3c5411980ea7536efa41c34aa9a70c0d708f02d6aad395d9de42f0d729c433a6
BLAKE2b-256 checksum
How to use checksums
03f45386166ef44077df09f0ceeeedf60f9dfb79b788d5752f77e58d30327b77
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.1.0 CPython/3.12.4

Release history Release notifications | RSS feed

This release

0.3 This release

2 release files

0.2

2 release files

0.1

2 release files

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