A lightweight scheduler that runs tasks in the background
Project description
PyBackground
PyBackground is
- a lightweight scheduler that runs tasks in the background
- written in Python (3.7+) Standard 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:
import time
def 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:
import pybackground
sched = pybackground.BackgroundScheduler()
sched.start(now, args=(1,))
sched.start(utcnow, args=(1,))
Shutdown the scheduler:
sched.shutdown(wait=True)
Handle with the infinite loops
Let's work based on now(cost)
as an example:
import pybackground
sched = 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:
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:
import pybackground
sched = 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:
sched.shutdown(wait=True)
Install PyBackground
$ pip install pybackground
Documentation
BackgroundScheduler
/BlockingScheduler
class pybackground.BackgroundScheduler/BlockingScheduler(max_worker=<num_cpu_cores>)
max_worker
is set for ThreadPoolExecutor
, default value is the number of CPU cores.
-
stopped
The scheduler is stopped or not,
True
(default) orFalse
. -
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
object dictionary,{}
as default. -
start(fn, args=(), kwargs={}, timeout=TIMEOUT)
Let scheduler start executing your function using thread pool in the background (or foreground), default value of
timeout
isTIMEOUT
, 3 seconds. It returns corresponding task id. -
shutdown(wait=True)
Shutdown the scheduler.
task
class pybackground.task(scheduler, timeout=TIMEOUT)
timeout
is set for function executing, default value is TIMEOUT
, 3 seconds.
-
Use
@task
decorator to define your functions and start executing them:@task(scheduler) def fn(args, kwargs): pass fn.start(*args, **kwargs)
fn.start(*args, **kwargs)
is equivaluent tosheduler.start(fn, args, kwargs)
using normal function definition.
Related Projects
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 Distributions
Built Distribution
Hashes for PyBackground-0.1.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3d12e509370c2d737ffa4c563020bf81584a500412c3d20f689d5c2206cba677 |
|
MD5 | f0f33099228e977fb7ebe06597b762ff |
|
BLAKE2b-256 | 8ff72fd9fd7d095b6cd7562ece57c58ea9b6576580ecd5fb0917ded46ab1880d |