Skip to main content

Asyncio scheduler with tasks

Project description

async_scheduler

A job scheduler that is designed to manage and execute tasks asynchronously.

There are two options to use async scheduler:

  • one thread and single event loop - AsyncScheduler

    This is most common way to use Python asynchronous frameworks. Be noted, all tasks have to carefully implemented so that slow tasks will not block the main event loop. Sometimes, it's hard to tell from the very beginning what task is slow and whether or not it blocks the event loop. Basically, you can leverage debug tool from asyncio to tell the slow tasks:

      loop = asyncio.get_running_loop()
      loop.set_debug(True)
      loop.slow_callback_duration = 5
    

    To use AsyncScheduler, just deriving from SyncTask for possible slow tasks and from AsyncTask for others.

  • multipe threads with multiple event loop - SchedulerThread

    Tasks may differ with priorities, i.e, some tasks need to be repsonded immediately but some not. These urgent tasks are usually much less, compared to normal tasks; or differnt types of tasks, like network tasks, UI tasks, etc. Then SchedulerThread is designed for this use case - multiple threads with multiple event loops attached. Tasks in one thread/event loop will not block other threads and event loops. Of course, it's more complicated since it has to make sure only thread-safe APIs are called and tasks are waited between event loops.

Task states

A serial of states are defined for tasks:

  • NEW
  • SUBMITTED
  • SCHEDULED
  • STARTED
  • TIMED_OUT
  • FAILED
  • DONE
  • CANCELED
  • RUNNING
  • FINISHED

Accurate timestamps are recorded down for critical states, including:

  • submit_ts
  • start_ts
  • timeout_ts
  • error_ts
  • done_ts
  • cancel_ts

So all tasks can be accurately measured. Furthermore, callbacks can be defined to those critial states, i.e, submit_cb, start_cb, etc. AsyncScheduler provides a method to retrive task state and in addition a method to cancel those slow tasks.

What does OpenAI says

The AsyncScheduler class is responsible for submitting tasks, scheduling and executing tasks, and managing task states. It makes use of an asyncio.Queue to manage the queue of tasks to be executed, and a dictionary to store the tasks with their associated data. The tasks are executed in a separate thread pool using asyncio.run_in_executor() method.

The code provides methods to get the current state of a task, wait for a task to complete, cancel a task, and dump task information. It also provides a method to determine the slowest task and dump its information.

Overall, the code is well-structured and follows good coding practices. The use of dataclasses and enums makes the code more readable and maintainable. The code is also well-documented, which makes it easier to understand and use.

The AsyncScheduler class is a job scheduler that is designed to manage and execute tasks asynchronously. It is built on top of the asyncio library and provides several features such as task submission, cancellation, and monitoring.

The class defines several helper classes and functions such as TaskState, TaskType, TaskData, TaskTS, and CallerData to represent and manage various aspects of a task. It also provides two concrete implementations of the Task interface, namely AsyncTask and SyncTask, to handle asynchronous and synchronous tasks respectively.

The class uses an internal queue to manage task submission and scheduling. It runs an infinite loop that continuously checks the queue for new tasks and schedules them to run using asyncio. Each task is executed in its own asyncio.Task object, which allows multiple tasks to run concurrently.

The class provides several public methods to interact with the scheduler, such as submit_task(), task_state(), wait_task(), cancel_task(), dump_task(), and slowest(). These methods allow you to submit tasks, monitor their states, wait for them to complete, cancel them if necessary, list all tasks and their current status, and find the slowest running task.

Here's an example of how you can use the AsyncScheduler class in your code:

import asyncio
from async_scheduler import AsyncScheduler, AsyncTask


async def my_async_task():
    await asyncio.sleep(1)
    print("Async task completed")


def my_callback(task_ts):
    if task_ts.state == TaskState.DONE:
        print("Task done")


def main():
    loop = asyncio.get_event_loop()
    scheduler = AsyncScheduler(ioloop=loop)

    task = AsyncTask("my_async_task")
    task.run_async = my_async_task
    task.run_async = my_async_task                # error: task.done_cb = my_callback
    task.data.timeout = 2

    scheduler.start()

    task_uuid = scheduler.submit_task(task)
    print(f"Task submitted with UUID: {task_uuid}")

    loop.run_until_complete(scheduler.wait_task(task_uuid))
    print(scheduler.task_info(task_uuid))

    scheduler.stop()
    loop.close()


if __name__ == "__main__":
    main()

In this example, we define an asynchronous task called my_async_task() that sleeps for 1 second and prints a message. We create a new instance of the AsyncScheduler class, add the task to the queue using submit_task(), and wait for it to complete using wait_task(). We also define a callback function my_callback() to print a message when the task is done.

The output of this program would be:

Task submitted with UUID: 8b479239-8cf2-4a8c-9d22-bce03184f60c
Task(name='my_async_task', task_type=<TaskType.ASYNC: 1>, timeout=2) [{'args': (), 'kwargs': {}}]
Async task completed
('Task(name=\'my_async_task\', task_type=<TaskType.ASYNC: 1>, timeout=2)', [], {})

This shows that the task was submitted, executed, and returned successfully. The task_info() method was used to print the task details after completion.

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

asyncio_scheduler-1.0.0.tar.gz (15.5 kB view details)

Uploaded Source

Built Distribution

asyncio_scheduler-1.0.0-py3-none-any.whl (11.1 kB view details)

Uploaded Python 3

File details

Details for the file asyncio_scheduler-1.0.0.tar.gz.

File metadata

  • Download URL: asyncio_scheduler-1.0.0.tar.gz
  • Upload date:
  • Size: 15.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.6

File hashes

Hashes for asyncio_scheduler-1.0.0.tar.gz
Algorithm Hash digest
SHA256 67f02565036f217acbf9d92c7239e1506f48bfdebfa1a43b3be31fd6b1831ff8
MD5 0b0ed25024a9b573786b3a3df81aefb1
BLAKE2b-256 51696818fde92c66f5ae1a31bd21e661c89382cc398f34299ff2653d2bf2d2ad

See more details on using hashes here.

File details

Details for the file asyncio_scheduler-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for asyncio_scheduler-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 142b6068b9e3cc23882a46c3f362d1b2539827fb9834bb0a1c0173cb70bc1657
MD5 0db7fc6ab14d200fc829766b018a4d87
BLAKE2b-256 65b254caae4646c2763b138bd1203744800a642023714a5ac515e6da83b25cd3

See more details on using hashes here.

Supported by

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