Skip to content

Feature: Add support for scheduling async coroutines #9

Description

@srics96

Feature Request

Add the ability to schedule async coroutines alongside regular functions. This is the most requested feature in the upstream project (29+ comments across multiple issues).

Desired API

import schedule
import asyncio

async def async_job():
    await some_async_operation()

# Schedule an async coroutine
schedule.every(10).seconds.do(async_job)

# Run with async support
async def main():
    while True:
        await schedule.async_run_pending()
        await asyncio.sleep(1)

asyncio.run(main())

Implementation Notes

  1. Add an async_run_pending() method to Scheduler that awaits coroutine jobs
  2. In Job.run(), detect if the job function is a coroutine (using asyncio.iscoroutinefunction()) and handle it appropriately
  3. Keep backward compatibility — regular (sync) jobs must continue to work exactly as before
  4. Add run_pending() support for mixed sync/async jobs (sync jobs run normally, async jobs are awaited)
  5. Do NOT add any external dependencies — use only Python's built-in asyncio module
  6. Add comprehensive tests covering:
    • Scheduling and running async jobs
    • Mixed sync and async jobs in the same scheduler
    • CancelJob return value from async jobs
    • Error handling in async jobs

References

Metadata

Metadata

Assignees

No one assigned

    Labels

    approvedPlan approved, implementation in progress

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions