Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/imyizhang/pybackground
A lightweight scheduler that runs tasks in the background
https://github.com/imyizhang/pybackground
Last synced: 10 days ago
JSON representation
A lightweight scheduler that runs tasks in the background
- Host: GitHub
- URL: https://github.com/imyizhang/pybackground
- Owner: imyizhang
- License: mit
- Created: 2020-12-12T03:11:15.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2020-12-15T07:53:45.000Z (about 4 years ago)
- Last Synced: 2024-11-07T22:08:47.373Z (2 months ago)
- Language: Python
- Homepage: https://pypi.org/project/PyBackground/
- Size: 9.77 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# PyBackground
PyBackground is
* a lightweight scheduler that runs tasks in the background
* written in [Python (3.7+) Standard Library](https://docs.python.org/3.7/library/)PyBackground supports to
* execute tasks using thread pool
* run in the background (or foreground)
* use `@task` decorator to define task## Quickstart
Define your functions:
```python
import timedef now(cost=1):
time.sleep(cost)
print( time.strftime('%Y-%m-%d %H:%M:%S %Z', time.localtime()) )
def utcnow(cost=1):
time.sleep(cost)
print( time.strftime('%Y-%m-%d %H:%M:%S %Z', time.gmtime()) )
```Create a PyBackground scheduler and start executing your functions:
```python
import pybackgroundsched = pybackground.BackgroundScheduler()
sched.start(now, args=(1,))
sched.start(utcnow, args=(1,))
```Shutdown the scheduler:
```python
sched.shutdown(wait=True)
```### Handle with the infinite loops
Let's work based on `now(cost)` as an example:
```python
import pybackgroundsched = pybackground.BackgroundScheduler()
print(sched.stopped)def timer(interval=3):
while not sched.stopped:
now()sched.start(timer, args=(3,))
````timer(interval)` then runs forever in a seperate thread. When you'd like to terminate it, shutdown the scheduler as usual:
```python
sched.shutdown(wait=True)
```### Play with the `@task` decorator
Use `@task` decorator to define your functions and start executing them, scheduling `now(cost)` and `utcnow(cost)` as an example:
```python
import pybackgroundsched = pybackground.BackgroundScheduler()
import time
@pybackground.task(sched)
def now(cost=1):
time.sleep(cost)
print( time.strftime('%Y-%m-%d %H:%M:%S %Z', time.localtime()) )
now.start(cost=1)@pybackground.task(sched)
def utcnow(cost=1):
time.sleep(cost)
print( time.strftime('%Y-%m-%d %H:%M:%S %Z', time.gmtime()) )
utcnow.start(cost=1)
```Shutdown the scheduler in normal way:
```python
sched.shutdown(wait=True)
```### Install PyBackground
```bash
$ pip install pybackground
```## Documentation
### `BackgroundScheduler`/`BlockingScheduler`
```python
class pybackground.BackgroundScheduler/BlockingScheduler(max_worker=)
````max_worker` is set for `ThreadPoolExecutor`, default value is the number of CPU cores.
* `stopped`
The scheduler is stopped or not, `True` (default) or `False`.
* `latest_id`
The latest task id, which may be useful for `pybackground.BlockingScheduler`.
* `task`
The task id, `Task` object (`collections.namedtuple('Task', 'fn, args, kwargs')`) dictionary, `{}` as default.
* `future`
The task id, [`Future`](https://docs.python.org/3/library/concurrent.futures.html#concurrent.futures.Future) object dictionary, `{}` as default.
* `start(fn, args=(), kwargs={})`
Let scheduler start executing your function using thread pool in the background (or foreground). It returns corresponding task id.
* `shutdown(wait=True)`
Shutdown the scheduler.
### `task`
```python
class pybackground.task(scheduler)
```* Use `@task` decorator to define your functions and start executing them:
```python
@task(scheduler)
def fn(args, kwargs):
pass
fn.start(*args, **kwargs)
````fn.start(*args, **kwargs)` is equivaluent to `sheduler.start(fn, args, kwargs)` using normal function definition.
## Related Projects
* [APScheduler](https://github.com/agronholm/apscheduler) ([apscheduler.readthedocs.org](http://apscheduler.readthedocs.org))