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
@taskdecorator 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.
-
stoppedThe scheduler is stopped or not,
True(default) orFalse. -
latest_idThe latest task id, which may be useful for
pybackground.BlockingScheduler. -
taskThe task id,
Taskobject (collections.namedtuple('Task', 'fn, args, kwargs')) dictionary,{}as default. -
futureThe task id,
Futureobject 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
class pybackground.task(scheduler)
-
Use
@taskdecorator 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
Metadata
Release files for PyBackground 0.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| PyBackground-0.1.2.tar.gz | 3.6 kB | Without provenance | Details for PyBackground-0.1.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| PyBackground-0.1.2-py3-none-any.whl | Python 3 | none | any | Details for PyBackground-0.1.2-py3-none-any.whl |
Total release size: 8.5 kB
Release files / PyBackground-0.1.2.tar.gz
| Download URL | PyBackground-0.1.2.tar.gz |
|---|---|
| Size | 3.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a8025039a4236253e045a7e87af1013098412d068151b29ea3ad6f38c83b3650
|
|
BLAKE2b-256 checksum How to use checksums |
476826195c3f8870de985ddc5fe20ecc857c91965fb31939b05348e3e99ca5d8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.2.0 pkginfo/1.6.1 requests/2.23.0 setuptools/51.0.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.7
|
Release files / PyBackground-0.1.2-py3-none-any.whl
| Download URL | PyBackground-0.1.2-py3-none-any.whl |
|---|---|
| Size | 5.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
52537f7a2787d91dde9f781687588697b34ad63fc9288d425c39afc879f57b07
|
|
BLAKE2b-256 checksum How to use checksums |
dc19b731ffcaa6b7d665fb1da53aebf8f5d4972a4fb6caab04980a41a552da13
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.2.0 pkginfo/1.6.1 requests/2.23.0 setuptools/51.0.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.7
|
Security for PyBackground 0.1.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.