Skip to main content

a simple tasklist library for running tasks asynchronously or synchronously

Project description

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 ./

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

asynctasklist-0.3.tar.gz (7.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

asynctasklist-0.3-py2.py3-none-any.whl (7.7 kB view details)

Uploaded Python 2Python 3

File details

Details for the file asynctasklist-0.3.tar.gz.

File metadata

  • Download URL: asynctasklist-0.3.tar.gz
  • Upload date:
  • Size: 7.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.0 CPython/3.12.4

File hashes

Hashes for asynctasklist-0.3.tar.gz
Algorithm Hash digest
SHA256 6fa2f9e548b6d4a34bd6a5f379d4396973519293fd6eb35720dc8112936194a8
MD5 566fc94f072df5957be9e365c3f97755
BLAKE2b-256 fb68b67b95724ec4e9e4490ed87e2e801661b4688830a55a1fea9f824053d23e

See more details on using hashes here.

File details

Details for the file asynctasklist-0.3-py2.py3-none-any.whl.

File metadata

  • Download URL: asynctasklist-0.3-py2.py3-none-any.whl
  • Upload date:
  • Size: 7.7 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.0 CPython/3.12.4

File hashes

Hashes for asynctasklist-0.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 3c5411980ea7536efa41c34aa9a70c0d708f02d6aad395d9de42f0d729c433a6
MD5 f5c1e5a15317dfd81c073fbf53080cc0
BLAKE2b-256 03f45386166ef44077df09f0ceeeedf60f9dfb79b788d5752f77e58d30327b77

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page