GithubHelp home page GithubHelp logo

mattblack85 / aio-scheduler Goto Github PK

View Code? Open in Web Editor NEW
0.0 3.0 0.0 16 KB

schedule coroutines from an external thread into your event loop

License: MIT License

Python 100.00%
python python3 asyncio scheduling periodic-tasks

aio-scheduler's Introduction

aio-scheduler Build Status

Small library that want to be lightweight and offload coroutine scheduling to another thread running its own event loop.

Motivation

asyncio does not have a built-in way of building periodic tasks, the usual way is to have a coroutine running a while true loop:

async def my_coro():
    while True:
        ...do_stuff...
        asyncio.sleep(X)

this way the coroutine will do stuff every X seconds. This is not really a nice option cause the coroutine itself won't return, never, and closing loops become a bit more complicated. Also IMO a coroutine's scope should not be to related to do some job every X time, but just doing some job, period. If we want to do that job periodically somebody else should schedule it, that's where this scheduler chime in.

Usage

The scheduler is a thread running its own event loop

from aioscheduler import AsyncIOScheduler

scheduler = AsyncIOScheduler()
# Start the thread
scheduler.start()

calling .start() won't actually do nothing, in fact the scheduler is waiting with a blocking call for some tasks to be added. If you want to add a periodic task just call:

scheduler.add_periodic('task_name', coro, 1, event_loop)

Note that the event loop passed to the scheduler for a given periodic task is the event loop where the task will be scheduled to run periodically, not the scheduler event loop, this is possible thanks to the asyncio.run_coroutine_threadsafe() method

When add_periodic is call the thread starts its event loop and will try to dispatch periodically the task at given interval on the given event loop.

Before trying to scheduler a coroutine, the scheduler checks if the coroutine's event loop is still opened, if not the task is removed from the list of periodic tasks. If there is no more periodc tasks to be scheduled the scheduler will block again (and not consuming CPU) and will wait for some more work to come again.

aio-scheduler's People

Contributors

mattblack85 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.