Saltar a contenido

background

app.utils.background

https://github.com/kennethreitz/background/

version 0.2.1 commit #773c5d6 29 Dec 2020

License: ISC

Maintainers:

  • kennethreitz
  • ParthS007
  • tuxtimo

Background

It runs stuff in the background.

1
2
3
"An elegant decorator-based abstraction around Python 3's concurrent.futures ThreadPoolExecutor class"

— Simon Willison

This module makes it stupidly simple to run things in the background of your application, be it a CLI app, or a web app.

Basic Usage

.. code:: python

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
import time

import background


@background.task
def work():
    # Do something expensive here.
    time.sleep(10)


for _ in range(100):
    work()

Advanced Usage

.. code:: python

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
import time

import background

# Use 40 background threads.
background.n = 40


@background.task
def work():
    time.sleep(10)
    return "Done!"

@background.callback
def work_callback(future):
    print(future.result())


for _ in range(100):
    work()