Skip to main content

## Helpers for using Django from threads

Project description

Helpers for using Django from threads

When handling requests django manages database connection lifecycles for you. By default closing them after each request or keeping them alive allowing re-use for a specified amount of time when CONN_MAX_AGE is set so long as no errors are encountered.

Sometimes you want to do work outside of a web request. When the work is large and you would like to distribute it, likely to places other than where the web requests are served, there are systems like Celery. There are other cases though were you're just trying to do work outside of the request & response cycle even and it's lightweight enough that running a full-blown job queuing system and it's associated data store is too involved. You just want Threads or even better a ThreadPoolExecutor.

If you just jump straight in and use either of those you'll run into slow to manifest and tough to track down problems with broken database connections. This is because Django automatically opens a connection per-thread when a database is accessed and leaves them lying around in that thread indefinitely. This results in errors when the database has timed out the connection or it has otherwise encountered problems.

django_thread provides a this problem by implementing a Thread class that mimics Django's request connection handling and provides a ThreadPoolExecutor that does so around the invocations of submitted calls.

Usage

Thread

django_thread.Thread is a 100% drop-in replacement for threading.Thread. See threading.Thread for usage and documentation.

from django_thread import Thread


class ExampleThread(Thread):
    def run(self):
        for some_model in SomeModel.objects.filter(...):
            ...


thread = ExampleThread()
thread.start()
# do other things
thread.join()

ThreadPoolExecutor

django_thread.ThreadPoolExecutor is a 100% drop-in replacement for concurrent.futures.ThreadPoolExecutor. See concurrent.futures.ThreadPoolExecutor for usage and documentation.

from concurrent.futures import as_completed
from django_thread import ThreadPoolExecutor


def update_or_create_thing(name):
    thing, _ = Thing.objects.update_or_create(name=name)
    return thing.id


executor = ThreadPoolExecutor()

futures = []
for i in range(5):
    future = executor.submit(update_or_create_thing, f'Name i')
    futures.append(future)

ids = [f.result() for f in futures]

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_thread-0.0.1.tar.gz (3.6 kB view details)

Uploaded Source

Built Distribution

django_thread-0.0.1-py3-none-any.whl (3.8 kB view details)

Uploaded Python 3

File details

Details for the file django_thread-0.0.1.tar.gz.

File metadata

  • Download URL: django_thread-0.0.1.tar.gz
  • Upload date:
  • Size: 3.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for django_thread-0.0.1.tar.gz
Algorithm Hash digest
SHA256 276cc21f3b2d747f63999b0910609b3ec5371e019844441b561a5c064a6c52d7
MD5 5088adae57a9ca877652b7e872a5bdb2
BLAKE2b-256 4e04ee873a76f166f39fdae65d29e638ef222ad07c19205740bc97f7cff23c25

See more details on using hashes here.

File details

Details for the file django_thread-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: django_thread-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 3.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for django_thread-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a65398ded5cc99bb5a09cd4d32befef26662882e7993d5409e7824b82c0947f7
MD5 56e49ebdd5dd52ae91f8cc3d0083c7cd
BLAKE2b-256 db41643983d77b407259ef6e141f6e7bf7147acb0122884cb2ef5b9211602167

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page