descript goes here
Project description
asynctasklist
a simple tasklist library for running tasks pseudo-asynchronously
- note:
Installation
Basic TaskList Usage
from asynctasklist import TaskList, Task
import time
# initialize the tasklist variable
tasks = TaskList()
# template function to return a new instance of a function
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 run():
print("timeout is done")
pass
# add a new task to the list
tasks.add(makeTask(1000, run)) # print message after 1 second
tasks.add(makeTask(2000, run)) # print message 2 seconds after the first message
# if you want to run the tasklist truly asynchronously, run a new thread before the main program loop
import threading
def task_worker():
while True:
tasks.execute()
t = threading.Thread(target=task_worker, daemon=True) # set daemon to true to stop thread when program ends
t.start()
# if you want to run the tasklist alongside your main program, simply put `task.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")
break
pass
- 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
Development
To contribute to this library, clone the ropository, make changes, then submit a pull request
Now install the dependencies and test dependencies:
pip install -e '.[test]'
To run the tests:
pytest
Project details
Release history Release notifications | RSS feed
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.2.tar.gz
(7.4 kB
view hashes)
Built Distribution
Close
Hashes for asynctasklist-0.2-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ea1c0d4d5a2815ed9d448ea22e74f79020daa87efc76d72c8e69659d31cb7267 |
|
MD5 | b88898c44c0d9732d647a24b1b58da8c |
|
BLAKE2b-256 | 52d20f59bc069abd3facf98888b96468ef93eba82a896f876daf27a7a9c2965f |